Java连接数据库
package genconnection;import java.sql.*;
public class GenConnection {
private static Connection con = null;
private static String db_url =
// "jdbc:mysql://localhost/long?user=root&password=test";
"jdbc:mysql://219.149.122.155/ctc02049?user=ctc02049&password=FpfkNPKk";
static {
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
} catch (Exception e) {
System.out.println(e.toString());
}
}
public GenConnection() {
}
public static Connection GetConnection() {
if (con != null) {
boolean isopen = false;
try {
isopen = con.isClosed();
} catch (Exception e) {
System.out.println(e.toString());
}
if (!isopen)return con;
}
try {
con = DriverManager.getConnection(db_url);
return con;
} catch (Exception e) {
return null;
}
}
// public static Connection GenConnection() throws ClassNotFoundException,
// IllegalAccessException, InstantiationException, SQLException {
// Class.forName("org.gjt.mm.mysql.Driver").newInstance();
// String url =
// "jdbc:mysql://219.149.122.155/ctc02049?user=ctc02049&password=FpfkNPKk";
// Connection conn = DriverManager.getConnection(url);
// return con;
// }
public static void Conclose() {
// if (con != null) {
// try {
// con.close();
// System.out.println("数据库已关闭");
// } catch (SQLException ex) {
// ex.printStackTrace();
// }
// }
}
}
这是什么啊?:o
页:
[1]