时间:2021-07-01 10:21:17 帮助过:3人阅读
package com.com.Dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class BaseDao {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/xiangmuyi?user=root&password=root&useUnicode=true&characterEncoding=UTF8";
PreparedStatement state=null;
Connection conn=null;
ResultSet result=null;
public void cleaveDriver(){
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage()+"驱动异常");
}
}
public void createObject(String sql){
cleaveDriver();
try {
conn=DriverManager.getConnection(url);
state=conn.prepareStatement(sql);
} catch (SQLException e) {
System.out.println(e.getMessage()+"连接异常");
}
}
public void closeObject(){
try {
if(state!=null)
state.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
System.out.println(e.getMessage()+"关闭异常");
}
}
public ResultSet Selectxiangmuyi(String sql,ArrayList<Object> array){
createObject(sql);
try {
if(array!=null){
for(int i=0;i<array.size();i++){
state.setObject(i+1, array.get(i));
}
}
ResultSet result=state.executeQuery();
return result;
} catch (SQLException e) {
System.out.println(e.getMessage()+"查询异常");
return null;
}
}
public boolean Updatexiangmuyi(String sql,ArrayList<Object> array){
createObject(sql);
try {
if(array!=null){
for(int i=0;i<array.size();i++){
state.setObject(i+1, array.get(i));
}
}
int result=state.executeUpdate();
return result>0;
} catch (SQLException e) {
System.out.println(e.getMessage()+"增删改异常");
return false;
}finally{
closeObject();
}
}
}
JDBC
标签:null exe dstat get code object list state sql