jdbc工具类的抽取。

JdbcUtils类:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

//使用配置文件

public class JdbcUtils {

    private static final String DRIVERCLASS;
    private static final String URL;
    private static final String USERNAME;
    private static final String PASSWORD;
    //以下这个静态方法可以加载出来src底下的DbInfo.properties属性配置文件中的内容
    static{
        DRIVERCLASS=ResourceBundle.getBundle("DbInfo").getString("driver");
        URL=ResourceBundle.getBundle("DbInfo").getString("url");
        USERNAME=ResourceBundle.getBundle("DbInfo").getString("user");
        PASSWORD=ResourceBundle.getBundle("DbInfo").getString("password");
    }
    //该静态代码块是用来加载jdbc驱动的
    //1、加载驱动
    static{
        try {
            Class.forName(DRIVERCLASS);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException{

        //2、获取连接
        Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        return con;
    }

    //关闭操作
    public static void closeConnection(Connection conn){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void closeStatement(Statement stmt){
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void closeResultSet(ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

配置文件(Dbinfo.properties)

#########MySQL DBinfo#############
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
user=root
password=

测试类(TestUtils):

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import cn.hikcn.jdbctest.JdbcUtils;
public class TestUtils {

    public static void main(String[] args) throws SQLException {
        //1、得到一个Connection
        Connection conn = JdbcUtils.getConnection();

        //2、得到一个执行sql语句Statement
        Statement stmt = conn.createStatement();

        String sql = "select username,password from info";
        //3、执行sql得到结果集
        ResultSet rs = stmt.executeQuery(sql);
        //4、遍历结果集
        while(rs.next()){
            String username = rs.getString("username");
            String pwd = rs.getString("password");
            System.out.println("username = "+username+"\r"+"password = "+pwd);
        }
        //5、释放资源
        JdbcUtils.closeConnection(conn);
        JdbcUtils.closeStatement(stmt);
        JdbcUtils.closeResultSet(rs);

    }

}

你可能感兴趣的:(Java-EE)