JAVA--DbUtil

Util:

接口:
 

package com.wsh.utils;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public interface ExecuteInter {

    Statement execute(Connection connection) throws SQLException;
}

jdbcUtil:

package com.yanqiushi.utils;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class JdbcUtil {
    private static String FILE_NAME = "src/jdbc.properties";
    private static String url;
    private static String database;
    private static String user;
    private static String password;
    static{
        try {
            Properties properties = new Properties();
            properties.load(new FileReader(FILE_NAME));
            String driverName = properties.getProperty("driverName");
            url = properties.getProperty("url");
            database = properties.getProperty("database");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage() + "\n\t\t" + "请将配置文件jdbc.properties放置到src目录下");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        try {
            Connection connection = DriverManager.getConnection(url+database,user,password);
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void execute(ExecuteInter executeInter){
        try {
            Connection connection = getConnection();
            executeInter.execute(connection).close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void showRS(ResultSet resultSet){
        try {
            while (resultSet.next()){
                System.out.print(resultSet.getString(1) + "\t");
                System.out.print(resultSet.getString(2) + "\t");
                System.out.print(resultSet.getString(3) + "\t");
                System.out.print(resultSet.getString(4) + "\t");
                System.out.println();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(ResultSet resultSet,Statement statement,Connection connection){
        if (resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement != null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Statement statement,Connection connection){
        close(null,statement,connection);
    }
    public static void close(Connection connection){
        close(null,connection);
    }
}

User:

package com.yanqiushi.test;

public class User {
    private int uid;
    private String uname;
    private int age;
    private String loc;

    public User() {
    }

    public User(int uid, String uname, int age, String loc) {
        this.uid = uid;
        this.uname = uname;
        this.age = age;
        this.loc = loc;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", uname='" + uname + '\'' +
                ", age=" + age +
                ", loc='" + loc + '\'' +
                '}';
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }
}

TestQueryRunner:

package com.yanqiushi.test;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.Connection;
import java.sql.SQLException;

public class WhQueryRunner extends QueryRunner {


    @Override
    public <T> T query(Connection conn, String sql, ResultSetHandler<T> rsh) throws SQLException {
        // 开启事务
        conn.setAutoCommit(false);
        T t = super.query(conn,sql,rsh);
        // 提交事务
        conn.commit();
        conn.close();
        return t;
    }

    @Override
    public <T> T query(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException {
        conn.setAutoCommit(false);
        T t = super.query(conn,sql,rsh,params);
        conn.commit();
        conn.close();
        return t;
    }
}

 

package com.parddu.dao; import java.io.IOException; import java.sql.*; import java.util.Properties; /** * 数据库功能类 * @author parddu * @version Sep 29, 2010 9:49:31 AM */ class DButil { private String driver=null; //驱动 private String dbName=null; //数据库名 private String host=null; //主机名 private String point=null; //端口 private String userName=null; //登录帐号 private String userPass=null; //登录密码 private static DButil info = null; private DButil(){} /** * 初始化方法,加载数据库连接信息 * @throws IOException */ private static void init() throws IOException{ Properties prop = new Properties(); prop.load(DButil.class.getResourceAsStream("/db_config.properties")); info = new DButil(); info.driver = prop.getProperty("driver"); info.dbName = prop.getProperty("dbName"); info.host = prop.getProperty("host"); info.point = prop.getProperty("point"); info.userName = prop.getProperty("userName"); info.userPass = prop.getProperty("userPass"); } /** * 得到数据库连接对象 * @return 数据库连接对象 */ static Connection getConn(){ Connection conn=null; if(info == null){ try { init(); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } } if(info!=null){ try { Class.forName(info.driver); String url="jdbc:sqlserver://" + info.host + ":" + info.point + ";databaseName=" + info.dbName; conn=DriverManager.getConnection(url,info.userName,info.userPass); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } else{ throw new RuntimeException("读取数据库配置信息异常!"); } return conn; } /** * 关闭查询数据库访问对象 * @param rs 结果集 * @param st 上下文 * @param conn 连接对象 */ static void closeConn(ResultSet rs, Statement st,Connection conn){ try { rs.close(); } catch (Exception e) {} try { st.close(); } catch (Exception e) {} try { conn.close(); } catch (Exception e) {} } /** * 关闭增、删、该数据库访问对象 * @param st 上下文对象 * @param conn 连接对象 */ static void closeConn(Statement st ,Connection conn){ try{ st.close(); conn.close(); }catch(Exception e){} } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值