1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| /*
* MyDBConnection.java
*
* Created on 7 mars 2006, 16:18
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package guitp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFrame;
/**
*
* @author lole
*/
public class MyDBConnection {
/** Creates a new instance of MyDBConnection */
private static Connection connection;
private static ConnectDBDialog dbd;
private static String db;
private static String host;
private static String port;
private static String user;
private static String password;
/** Creates a new instance of MyDBConnection */
private MyDBConnection() {}
/*private MyDBConnection(String db, String host, String port, String user, String password) {
setParams(db, host, port, user, password);
}*/
private static Connection init(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e){System.out.println(e);}
try{
//System.out.println("Connection complete");
return DriverManager.getConnection(
"jdbc:mysql://" + host + ":" +port +"/" + db ,user , password
);
} catch(SQLException sqle){
System.out.println("Failed to get connection");
System.out.println("Exception SQL : ");
while (sqle != null) {
String message = sqle.getMessage();
String sqlState = sqle.getSQLState();
int errorCode = sqle.getErrorCode();
System.out.println("Message = "+message);
System.out.println("SQLState = "+sqlState);
System.out.println("ErrorCode = "+errorCode);
//sqle.printStackTrace();
sqle = sqle.getNextException();
}
return null;
}
}
public static void showConnectDialog(MainFrame fr) {
dbd = new ConnectDBDialog(fr);
}
public static void setParams(String dbs, String h, String p, String u, String pwd) {
db = dbs;
host = h;
port = p;
user = u;
password = pwd;
}
public static Connection getConnection(){
if (connection == null) {
connection = init();
}
return connection;
}
public static void destroy(){
if(connection !=null){
try{
connection.close();
} catch(Exception e){}
}
}
public static boolean isActive() {
if(connection !=null)
return true;
else
return false;
}
} |
Partager