/**
* 作爲所有Dao實現類的一個基類(父類)
*
* @author BluceYoung
*
*/
public class BaseDao {
// 加載驅動的字符串
static String driverClass = "com.mysql.jdbc.Driver";
// 數據庫字符串
static String url = "jdbc:mysql://localhost:3306/userhead?userUnicode=true&character=utf-8";
// 數據庫用戶名
static String user = "root";
// 密碼
static String pwd = "abc123";
static {
System.out.println("讀取配置文件");
Properties properties=new Properties();
try {
//創建一個讀取配置文件的輸入流
InputStream is=BaseDao.class.getClassLoader().getResourceAsStream("database.properties");
properties.load(is);//開始讀取文件裏的信息到properties對象中
driverClass=properties.getProperty("driver");
url=properties.getProperty("url");
user=properties.getProperty("name");
pwd=properties.getProperty("pwd");
} catch (IOException e) {
System.out.println("讀取配置文件出錯");
e.printStackTrace();
}
System.out.println("driver="+driverClass);
System.out.println("url="+url);
System.out.println("user="+user);
System.out.println("pwd="+pwd);
}
/**
* 獲取一個數據庫連接
*
* @return
*/
public Connection getConnection() {
// 1.加載驅動
try {
Class.forName(driverClass);
// 2.創建連接(自動打開)
Connection connection = DriverManager.getConnection(url, user, pwd);
return connection;
} catch (ClassNotFoundException e) {
System.out.println("加載驅動出錯,請檢查是否導入jar包,以及驅動類的拼寫");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("創建連接出錯,請檢查數據庫名、用戶名和密碼是否正確");
e.printStackTrace();
}
return null;
}
/**
* 執行一個帶參數的增刪改sql,返回影響行數
*
* @param sql 帶?參數的sql語句
* @param params sql語句裏需要替換的參數具體的值
* @return
*/
public int execute(String sql, Object... params) {
// 獲取一個連接
Connection connection = this.getConnection();
PreparedStatement stat=null;
// 根據sql和連接,獲取一個聲明
try {
stat= connection.prepareStatement(sql);
// 加入所有參數
for (int i = 0; i < params.length; i++) {
stat.setObject(i + 1, params[i]);
}
int hang = stat.executeUpdate();
return hang;
} catch (SQLException e) {
System.out.println("執行sql出錯");
System.out.println("執行的sql:" + sql);
System.out.println("傳入的參數是:");
for (int i = 0; i < params.length; i++) {
System.out.println(params[i]);
}
e.printStackTrace();
}finally {
this.closeAll(connection, stat, null);
}
return 0;
}
/**
* 執行插入語句,返回自增編號
* @param sql
* @param params
* @return
*/
public int executeInsert(String sql, Object... params) {
// 獲取一個連接
Connection connection = this.getConnection();
PreparedStatement stat = null;
ResultSet generatedKeys = null;
// 根據sql和連接,獲取一個聲明
try {
stat = connection.prepareStatement(sql);
// 加入所有參數
for (int i = 0; i < params.length; i++) {
stat.setObject(i + 1, params[i]);
}
int hang = stat.executeUpdate();
generatedKeys = stat.getGeneratedKeys();//查詢剛插入的自增列
if (generatedKeys.next()) {
return generatedKeys.getInt(1);//返回
}
} catch (SQLException e) {
System.out.println("執行sql出錯");
System.out.println("執行的sql:" + sql);
System.out.println("傳入的參數是:");
for (int i = 0; i < params.length; i++) {
System.out.println(params[i]);
}
e.printStackTrace();
} finally {
this.closeAll(connection, stat, generatedKeys);
}
return 0;
}
/**
* 釋放所有資源
*
* @param connection
* @param stat
* @param rs
*/
public void closeAll(Connection connection, PreparedStatement stat, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (stat != null) {
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}