packagecom.cc8w.dao;importjava.io.IOException;importjava.io.InputStream;importjava.lang.reflect.Field;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.ResultSetMetaData;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.ArrayList;importjava.util.Properties;/*** 数据库操作类
*@authorAdministrator
**/
public classJdbcDao {private Connection conn = null;private Statement st = null;//Statement
private PreparedStatement ps = null;//PreparedStatement
private ResultSet rs = null;//构造方法连接数据库信息
publicJdbcDao() {
Properties properties= newProperties();
InputStream is=null;try{//is = new FileInputStream("/db.properties");
is = this.getClass().getResourceAsStream("/db.properties");
properties.load(is);
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
System.exit(0);
}
String driver= properties.getProperty("jdbc.driver");
String host= properties.getProperty("jdbc.host");
String username= properties.getProperty("jdbc.username");
String password= properties.getProperty("jdbc.password");
String port= properties.getProperty("jdbc.port");try{
Class.forName(driver);this.conn =DriverManager.getConnection(host, username, password);this.st =conn.createStatement();
}catch (ClassNotFoundException |SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}//新增操作(返回真假)
public booleanadd(String sql,Object ... args)
{int flag = 0;try{
ps=conn.prepareStatement(sql);if(ps==null) System.exit(0);int i=1;for(Object o :args) {
ps.setObject(i, o);
i++;
}//res = ps.execute();//为什么不用即使成功了也返回false
flag = ps.executeUpdate();//成功返回1,失败返回0
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}if(flag==0) {return false;
}else{return true;
}
}//新增操作(返回新增行id)
public intaddInsertId(String sql ,Object ... args) {int flag = 0;try{
ps=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);if(ps==null) System.exit(0);int i=1;for(Object o :args) {
ps.setObject(i, o);
i++;
}
flag= ps.executeUpdate();//成功返回1,失败返回0
if(flag!=0) {
ResultSet rs=ps.getGeneratedKeys();if(rs.next()){return rs.getInt(1);
}
}
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}return 0;
}//更新操作
public booleanupdate(String sql,Object ... args) {int flag = 0;try{
ps=conn.prepareStatement(sql);if(ps==null) System.exit(0);int i=1;for(Object o :args) {
ps.setObject(i, o);
i++;
}
flag= ps.executeUpdate();//成功返回1,失败返回0
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}if(flag==0) {return false;
}else{return true;
}
}//删除操作(同上)
public booleandel(String sql,Object ... args) {int flag = 0;try{
ps=conn.prepareStatement(sql);if(ps==null) System.exit(0);int i=1;for(Object o :args) {
ps.setObject(i, o);
i++;
}
flag= ps.executeUpdate();//成功返回1,失败返回0
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}if(flag==0) {return false;
}else{return true;
}
}//查询一条记录
public T getOne(Classclazz,String sql,Object ... args) {
T t=null;try{
t=clazz.newInstance();
}catch (InstantiationException |IllegalAccessException e1) {//TODO Auto-generated catch block
e1.printStackTrace();
}try{//1.ps里面->查询出resultSet
ps=conn.prepareStatement(sql);if(ps==null) System.exit(0);int i=1;for(Object o :args) {
ps.setObject(i, o);
i++;
}//2.ps里面->ResultSetMetaData
rs = ps.executeQuery();//获取数据表头
ResultSetMetaData rsmd =ps.getMetaData();int colNum =rsmd.getColumnCount();
String []colLable= newString[colNum] ;for(i=1;i<=(colNum);i++) {
colLable[i-1] =rsmd.getColumnLabel(i);
}while(rs.next()){for(String dd :colLable) {
Field f=clazz.getDeclaredField(dd);
f.setAccessible(true);
f.set(t, rs.getObject(dd));
}continue; //跳出本次循环,只取一条记录
}
}catch (SQLException | NoSuchFieldException | SecurityException | IllegalArgumentException |IllegalAccessException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnt;
}//查询多条记录
public ArrayList getAll(Classclazz,String sql,Object ... args) {//ArrayList ss = new ArrayList();
ArrayList arrt = new ArrayList();
T t=null;try{//1.ps里面->查询出resultSet
ps =conn.prepareStatement(sql);if(ps==null) System.exit(0);int i=1;for(Object o :args) {
ps.setObject(i, o);
i++;
}//2.ps里面->ResultSetMetaData
rs = ps.executeQuery();//获取数据表头
ResultSetMetaData rsmd =ps.getMetaData();int colNum =rsmd.getColumnCount();
String []colLable= newString[colNum] ;for(i=1;i<=(colNum);i++) {
colLable[i-1] =rsmd.getColumnLabel(i);
}while(rs.next()){
t=clazz.newInstance();for(String dd :colLable) {
Field f=clazz.getDeclaredField(dd);
f.setAccessible(true);
f.set(t, rs.getObject(dd));
}
arrt.add(t);
}
}catch (SQLException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException |InstantiationException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnarrt;
}//返回任意值类型
@SuppressWarnings("unchecked")public V get(Object obj){return(V)obj;
}//数据库关闭
public voidclose() {try{if(rs!=null) rs.close();if(ps!=null) ps.close();if(st!=null) st.close();if(conn!=null) conn.close();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}//析构函数
public voidfinalize()
{
close();
}
}