日期:2014-05-16  浏览次数:20432 次

JDBC连接工具类


package com.ins.db;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DBconn {
	//declare var 
	private String driver="";
	private String url="";
	private String uname="";
	private String passwd="";
	private Connection conn=null;
	private Statement stmt=null;
	private static ResultSet rs=null;
	private static final String DATABASE_PROPERTIES="jdbc.properties";//定义配置文件
	
	//load properties
	public DBconn(){
		loadProperties();
		setConn();
	}
	//load conf
	private void loadProperties(){
		InputStream in =DBconn.class.getResourceAsStream(DATABASE_PROPERTIES);//读入配置文件
		//定义Properties
		Properties p=new Properties();
		try {
			p.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//得到配置文件并将其值赋给当前变量
		this.driver=p.getProperty("driver");
		this.url=p.getProperty("url");
		this.uname=p.getProperty("username");
		this.passwd=p.getProperty("password");
	}
	//setConn
	private void setConn(){
		try {
			Class.forName(driver);  
			this.conn=DriverManager.getConnection(url,uname,passwd);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	//getConn
	public  Connection getConn(){
		return this.conn;
	}
	
//=================================CRUD===========================
	//执行插入
	public  void doInsert(String sql){
		try {
			stmt=conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			System.err.println("db.executeInset:" + e.getMessage());   
		}finally{
			colseAll(rs,stmt,conn);
		}
	}
	//	执行删除
	public  void doDelete(String sql){
		try {
			stmt=conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			System.err.println("db.executeInset:" + e.getMessage());   
		}finally{
			colseAll(rs,stmt,conn);
		}
	}
	
//	执行更新
	public  void doUpdate(String sql){
		try {
			stmt=conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			System.err.println("db.executeInset:" + e.getMessage());   
		}finally{
			colseAll(rs,stmt,conn);
		}
	}
//	查询结果集   
    public  ResultSet doSelect(String sql) {   
        try {   
            stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);   
            rs = stmt.executeQuery(sql);   
        } catch(SQLException sqlexception) {   
            System.err.println("db.executeQuery: " + sqlexception.getMessage());   
        }   
        return rs;   
    } 
    
//=============================================CRUD end========================
	//close rs
	public  static void colseRs(ResultSet rs){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			rs=null;
		}
	}
//	close stmt
	public  static void colseStmt(Statement stmt){
		if(stmt!=null){
			try {
				stmt.close();
			} catch (SQLException es) {
				es.printStackTrace();
			}
			stmt=null;
		}
	}
//	close conn
	public  void colseConn(Connection conn){
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException ec) {
				ec.printStackTrace();
			}
			conn=null;
		}
	}
//close all
	public static void colseAll(ResultSet rs,Statement stmt,Connection conn){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			rs=null;
		}
		if(stmt!=null){
			try {
				stmt.close();
			} catch (SQLException es) {
				es.printStackTrace();
			}
			stmt=null;
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException ec) {
				ec.printStackTrace();
			}
			conn=null;
		}
	}
	public static void main(String[] args) {
		DBconn db=new DBconn();
		String sql="select *from class";
		rs=db.doSelect(sql);
		
		try {