java用一个类连接数据库_自己写的一个java链接数据库的类

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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值