1. 程式人生 > >(mysql版)德魯伊連線池的配置方法

(mysql版)德魯伊連線池的配置方法

Mysql的連線池配置方法


  • 先去下載兩個包,匯入bin中百度網盤連結,密碼:n3en
  • 在javaweb專案中的src裡面建立一個字尾為:properties 的File檔案,然後在此檔案頁面中寫下一下程式碼:
username=root
password=123456
maxActive=100
maxWait=10000
maxIdle=1
url=jdbc\:mysql\://localhost\:3306/stuD
driverClassName=com.mysql.jdbc.Driver
  • 在建立一個util包,建立DBUtil類中寫一下程式碼:
package com.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;


import com.alibaba.druid.pool.DruidDataSource;


public class DBUtil {
private DruidDataSource ds = null;

Connection conn=null
; ResultSet rs=null; PreparedStatement pst=null; public DBUtil(){ ResourceBundle rb=ResourceBundle.getBundle("prop"); String username=rb.getString("username"); String password = rb.getString("password"); String maxActive = rb.getString("maxActive"); String maxWait = rb.getString("maxWait"); String maxIdle = rb.getString("maxIdle"
); String url = rb.getString("url"); String driverClassName = rb.getString("driverClassName"); //建立資料來源物件 DruidDataSource dbs = new DruidDataSource(); dbs.setUsername(username); dbs.setPassword(password); dbs.setMaxActive(Integer.parseInt(maxActive)); dbs.setMaxWait(Integer.parseInt(maxWait)); dbs.setMinIdle(Integer.parseInt(maxIdle)); dbs.setUrl(url); dbs.setDriverClassName(driverClassName); ds=(DruidDataSource)dbs; } /** * 開啟連線池 */ public void getConn(){ try { conn=ds.getConnection(); } catch (Exception e) { e.printStackTrace(); } } /** * 查詢資料 * @param sql * @param params * @return */ public ResultSet execQuery(String sql,Object[] params){ getConn(); try { pst = conn.prepareStatement(sql); if(params!=null){ for(int i=0;i<params.length;i++){ pst.setObject(i+1, params[i]); } } rs = pst.executeQuery(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs; } /** * 增刪改 * @param sql * @param obj * @return */ public int getUpdate(String sql,Object[] obj){ int count=0; //開啟資料庫連結物件 this.getConn(); try { //執行sql語句 pst=conn.prepareStatement(sql); //判斷是否有引數傳過來 if(obj!=null){ for(int i=0;i<obj.length;i++){ //賦值 pst.setObject(i+1,obj[i]);//這裡i+1是因為,賦值的時候是從第一位開始的 } } count=pst.executeUpdate(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ this.getClose(null); } return count; } /** * 關閉連線物件 */ public void getClose(ResultSet rs){ try { if(conn!=null){ conn.close(); } if(rs!=null){ rs.close(); } if(pst!=null){ pst.close(); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }
  • 完成以上兩部操作,就可以直接去dao方法中寫方法查詢和增刪改了,如下例子:
package com.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


import com.entity.stuInfo;
import com.util.DBUtil;


public class stuInfoDao {

DBUtil util=new DBUtil();
Connection conn=null;
ResultSet rs=null;
PreparedStatement pat=null;
/***
* 查詢所有學生資訊
* @param stu
* @return
*/
public ArrayList<stuInfo>Query(){
ArrayList<stuInfo> list=new ArrayList<stuInfo>();
String sql="select*from stuInfo;";
rs=util.execQuery(sql,null);
try {
while(rs.next()){
//封裝實體類
stuInfo studen=new stuInfo(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getString(5));
//新增到集合
list.add(studen);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
util.getClose(rs);
}
return list;
}
}