java jdbc操作类_Java-编写一个jdbc操作类

package数据库操作类;/** Db.java

Created on 2007年8月20日, 上午 8:37*/

import java.io.*;import java.sql.*;importjava.util.Properties;public classDB {privateString driver;privateString url;privateString user;privateString password;privateConnection conn;privateStatement stm;privateResultSet rs;publicDB(){this("DBConf.properties");

}publicDB(String conf) {

loadProperties(conf);

setConn();

}publicConnection getConn(){return this.conn;

}//handle the properties file to get the informations for connection

private voidloadProperties(String conf){

Properties props= newProperties();try{

props.load(newFileInputStream(conf));

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}this.driver = props.getProperty("driver");this.url = props.getProperty("url");this.user = props.getProperty("user");this.password = props.getProperty("password");

}//implement the Connection

private voidsetConn(){try{

Class.forName(driver);this.conn =DriverManager.getConnection(url,user,password);

}catch(ClassNotFoundException classnotfoundexception) {

classnotfoundexception.printStackTrace();

System.err.println("db: " +classnotfoundexception.getMessage());

}catch(SQLException sqlexception) {

System.err.println("db.getconn(): " +sqlexception.getMessage());

}

}public voiddoInsert(String sql) {try{

Statement statement=conn.createStatement();int i =stm.executeUpdate(sql);

}catch(SQLException sqlexception) {

System.err.println("db.executeInset:" +sqlexception.getMessage());

}

}public voiddoDelete(String sql) {try{

stm=conn.createStatement();int i =stm.executeUpdate(sql);

}catch(SQLException sqlexception) {

System.err.println("db.executeDelete:" +sqlexception.getMessage());

}

}public voiddoUpdate(String sql) {try{

stm=conn.createStatement();int i =stm.executeUpdate(sql);

}catch(SQLException sqlexception) {

System.err.println("db.executeUpdate:" +sqlexception.getMessage());

}

}publicResultSet doSelect(String sql) {try{

stm=conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);

rs=stm.executeQuery(sql);

}catch(SQLException sqlexception) {

System.err.println("db.executeQuery: " +sqlexception.getMessage());

}returnrs;

}public static voidmain(String[] args){try{

DB db= newDB();

Connection conn=db.getConn();if(conn != null && !conn.isClosed()) {

System.out.println("連結成功");

ResultSet rs= db.doSelect("select * from content");while(rs.next()){

System.out.println(rs.getString(1)+":"+rs.getString(2)+":"+rs.getString(3));

}

rs.close();

conn.close();

}

}catch(SQLException e) {

e.printStackTrace();

}

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值