import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class Database {
private Connection conn = null;
private Statement stmt;
private ResultSet rs;
private ResultSetMetaData rsmd;
public void connect() {
try {
//datenbanktreiber laden
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(
"jdbc:mysql://URL zum SERVER:PORT/Datenbank",
"USER", "Password");
this.stmt = this.conn.createStatement();
} catch (SQLException sqle) {
System.out.println("SQLException: " + sqle.getMessage());
System.out.println("SQLState: " + sqle.getSQLState());
System.out.println("VendorError: " + sqle.getErrorCode());
sqle.printStackTrace();
}
}
//SQL Befehl wie Select ausführen
public void execute(String querry){
try {
this.rs = this.stmt.executeQuery(querry);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Update ausführen da dort executeUpdatebenötigt wird
public void update(String query){
try {
this.stmt.executeUpdate(query);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//alles schliessen
public void free(){
try {
if(!(this.rs==null)){
rs.close();
}
if(!(this.stmt==null)){
this.stmt.close();
}
if(!(this.conn==null)){
this.conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void closeRS(){
if(!(this.rs==null)){
try {
this.rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void closeSmt(){
if(!(this.stmt==null)){
try {
this.stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//ergebnisse zurückgeben
public ResultSet getResultSet(){
return this.rs;
}
}