日期:2014-05-16  浏览次数:20756 次

连接mysql的工具类

DBHelper.java:

package com.syyz.emp.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


//mysql数据库连接的工具类
public class DBHelper {

private  static DBHelper db = null;

private static final String DBDRIVER = "com.mysql.jdbc.Driver";
private static final String DBURL = "jdbc:mysql://localhost:3306/emp";
private static final String DBUSER = "root";
private static final String DBPASSWORD = "123456";

private static Connection conn = null;

public DBHelper(){

}

public static DBHelper getDB(){
if(db == null){
db = new DBHelper();
}
return db;
}

public  Connection getConnection() {
try {
Class.forName(DBDRIVER);//加载数据库驱动程序
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}

return conn;
}


}