J2EE连接数据库
public class connect {
private static String driver=“oracle.jdbc.driver.OracleDriver”;
private static String url=“jdbc:oracle:thin:@localhost:1521:XE”;
private static String user=“自己填”;
private static String password=“自己填”;
/***
* 连接数据库
/
public static Connection getCon() throws ClassNotFoundException, SQLException{
Class.forName(driver);
System.out.println(“测试加载数据库成功”);
Connection con=DriverManager.getConnection(url, user, password);
System.out.println(“测试数据库链接成功”);
return con;
}
/**
* 关闭数据库
*/
public static void close(Connection con,PreparedStatement ps,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}