package dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
//负责数据库的增删改查操作
public class BaseDao {
private static final String URI= "jdbc:mysql://localhost:3306/date2?"+"user=root&password=root&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";//换成你自己的数据库名字和密码
private static final String DRIVER="com.mysql.cj.jdbc.Driver";
/*public static Connection connectDB()throws Exception {
//加载数据库驱动
Class.forName(DRIVER);
//获取数据库连接
Connection conn= DriverManager.getConnection(URI);
return conn;
}*/
//存放Connection对象的数组,数组被看成连接池
static ArrayList<Connection> list = new ArrayList<Connection>();
//获得链接
public synchronized static Connection getConnection() {
Connection con = null;
//如果连接池中有连接对象
if (list.size() > 0) {
return list.remove (0) ;}
//连接池没有连接对象,创建连接放到连接池中
else{
for(int i=0;i<5;i++){
try{
Class. forName (DRIVER);
} catch (ClassNotFoundException e){
e.printStackTrace() ;}
//创建连接
try {
con = DriverManager. getConnection ("jdbc:mysql://localhost:3306/date2?characterEncoding=utf-8","root","200068") ;
list.add(con);
}catch (SQLException e){
e.printStackTrace() ;
}
}
}
return list.remove(0);
}
//关闭结果集
public static void close(ResultSet rs){
if( rs!=null){
try{
rs.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
//关闭预处理
public static void close(PreparedStatement pst){
if( pst!=null){
try{
pst.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
//关闭连接
public static synchronized void close(Connection con){
if( con!=null){
list.add(con);
}
}
//关闭所有连接有关的有对对象
public static void close(ResultSet rs,PreparedStatement ps,Connection con) {
close(rs);
close(ps);
close(con);
}
//新增,修改,删除处理
public boolean updateByParams(String sql,Object param[]){
boolean flag=false;
Connection con=getConnection();
PreparedStatement ps=null;
try{
ps=con.prepareStatement(sql);
if(param!=null){
for(int i=1;i<=param.length;i++) {
ps.setObject(i, param[i - 1]);
}
}
int n=ps.executeUpdate();
if(n>0)
flag=true;
}catch (SQLException e){
e.printStackTrace();
}finally {
close(null,ps,con);
}
return flag;
}
//新增,修改,删除处理(批量)
public boolean BatchUpdateByParams(String sql,Object param[][]){
Connection con=getConnection();
PreparedStatement ps=null;
try{
ps=con.prepareStatement(sql);
if(param!=null){
for(int i=0;i<=param.length;i++) {
for (int j = 1; j <= param[i].length; i++) {
ps.setObject(j, param[i][j - 1]);
}
ps.addBatch();
}
ps.executeBatch();
}
return true;
}catch (SQLException e){
e.printStackTrace();
return false;
}finally {
close(null,ps,con);
}
}
//查询
public static List<Map<String,Object>> select (String sql, Object[] param){
Connection con =getConnection();
PreparedStatement ps=null ;
ResultSet rs = null;
List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();
try{
ps = con.prepareStatement (sql) ;
if (param!= null) {
for (int i = 1; i <= param. length; i++) {
ps.setObject(i, param[i-1]) ;
}
}
rs = ps.executeQuery();
ResultSetMetaData rm=rs.getMetaData();
//列数
int count=rm.getColumnCount();
while (rs.next()) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 1; i <= count; i ++) {
//key为列名, value为列值
map.put(rm.getColumnName(i).toLowerCase(), rs.getObject(rm.getColumnName(i)));
}
list.add(map);}
}catch (SQLException e) {
e.printStackTrace();
}finally{
close(rs, ps, con);
}
return list;
}
//获取最后一个ID
public int getLastId(String sql, String sqll, Object[] param) {
Connection con = getConnection() ;
PreparedStatement ps = null;
PreparedStatement ps1 = null;
ResultSet rs = null;
int id=0;
try{
ps = con.prepareStatement (sql);
if(param != null) {
for(int i=1; i <=param. length; i++){
ps.setObject(i, param[i-1]);}
}
ps.executeUpdate();
ps1=con.prepareStatement(sqll);
rs= ps1.executeQuery();
if(rs.next())
id= rs.getInt(1);
close (ps1);
}catch (SQLException e){
e.printStackTrace();
}finally{
close(rs, ps,con) ;}
return id;
}
}
BaseDao包(含连接数据库)
最新推荐文章于 2021-03-20 20:42:58 发布