Det er bare helt iorden...her kommer der to klasser - en DatabaseConnection-klasse, og en DatabaseFacade-klasse:
package control;
import java.sql.*;
/**
*
* @author Michael Dipo
*/
public class DataBaseConnection {
private static DataBaseConnection INSTANCE = new DataBaseConnection();
private Connection conn = null;
public static DataBaseConnection getInstance(){
return INSTANCE;
}
/** Creates a new instance of DataBaseConnection */
private DataBaseConnection() {
try{
Class.forName("org.gjt.mm.mysql.Driver");
} catch (ClassNotFoundException cnfe){
System.err.println("ClassNotFound"+ cnfe);
}
String host = "localhost";
String dbName = "eshop";
int port = 3306;
String uName = "root";
String pWord = "michael";
try{
conn = DriverManager.getConnection("jdbc:
mysql://"+host+"/"+dbName+"?user="+uName+"&password="+pWord);
} catch (SQLException SQLE){
System.out.println("SQLException" + SQLE);
}
}
public Connection getConnection(){
return conn;
}
}
------------------------------------------------------------------
public class DatabaseFacade {
/** Creates a new instance of DatabaseHandler */
public DatabaseFacade() {
}
public ResultSet findMedlem(String medlemsId)throws SQLException{
Connection conn = DataBaseConnection.getInstance().getConnection();
Statement stat = conn.CreateStatement();
ResultSet rs = stat.executeQuery("SELECT *
FROM MEDLEM WHERE MEDLEMSID='"+MEDLEMSID+"'");
/*Her kan du alternativt behandle ResultSet og istedet returnere en String eller et Medlem-objekt hvis det er nødvendigt - ideen er så, at du kalder denne metode fra din GUI3*/
return rs;
}