简单实现jdbc连接数据库通用工具类(代码)

简单实现jdbc连接数据库通用工具类

package util;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class DbUtil {


private static String url="jdbc:mysql//localhost:3306/db_book";
private static String user="root";
private static String password="root";
private static String mysql_driver="com.mysq.jdbc.driver";
public Connection getCon(){
Connection con=null;
try {
Class.forName(mysql_driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con=DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();  
}
return con;
}
public void closeCon(Connection con){
if(con!=null)
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

你可能感兴趣的:(JDBC)