数据库JDBC&properties配置文件工具类及测试

JDBC工具类及测试

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*
 * JDBCUtils工具类代码
 */
public class JDBCUtils {
    private JDBCUtils(){}
    private static Connection con;
    
    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/数据库表名";
            String user = "数据库连接用户名";
            String password = "数据库连接密码";
            con = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            // TODO: handle exception
            throw new RuntimeException(e + "数据库连接失败");
        }
    }
    
    /*
     * 定义静态方法,返回数据库的连接对象
     */
    public static Connection getConnection(){
        return con;
    }
    
    /*
     * 关闭
     */
    public static void close(Connection con,Statement stat,ResultSet rs){
        if(rs!=null){
             try{
                 rs.close();
             }catch(SQLException ex){}
         }
         
         if(stat!=null){
             try{
                 stat.close();
             }catch(SQLException ex){}
         }
         
         if(con!=null){
             try{
                 con.close();
             }catch(SQLException ex){}
         }
    }
    
    public static void close(Connection con,Statement stat){
         
         if(stat!=null){
             try{
                 stat.close();
             }catch(SQLException ex){}
         }
         
         if(con!=null){
             try{
                 con.close();
             }catch(SQLException ex){}
         }
    }
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/*
 * 测试JDBCUtils工具类的代码
 */
public class TestJDBCUtils {
    public static void main(String[] args)throws Exception {
        Connection con = JDBCUtils.getConnection();
        PreparedStatement pst = con.prepareStatement("查询数据库表的SQL语句");
        ResultSet rs = pst.executeQuery();
        while(rs.next()){
            System.out.println(rs.getString("表中字段"));
        }
        JDBCUtils.close(con, pst, rs);
    }
}

properties配置文件

文件位置:任意,建议src下
文件名称:任意,扩展名为properties
database.properties

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/
user=
password=

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

/*
 *  编写数据库连接的工具类,JDBC工具类
 *  获取连接对象采用读取配置文件方式
 *  读取文件获取连接,执行一次,static{}
 */
public class JDBCUtilsConfig {
    private static Connection con ;
    private static String driverClass;
    private static String url;
    private static String user;
    private static String password;
    
    static{
        try{
            readConfig();
            Class.forName(driverClass);
            con = DriverManager.getConnection(url, user, password);
        }catch(Exception ex){
            throw new RuntimeException("数据库连接失败");
        }
    }
    
    private static void readConfig()throws Exception{
        InputStream in = JDBCUtilsConfig.class.getClassLoader().getResourceAsStream("database.properties");
        Properties pro = new Properties();
        pro.load(in);
        driverClass=pro.getProperty("driverClass");
        url = pro.getProperty("url");
        user = pro.getProperty("user");
        password = pro.getProperty("password");
    }
    
    
    public static Connection getConnection(){
        return con;
    }
}
/*
 *  测试工具类
 */
public class TestJDBCUtils {
    public static void main(String[] args) {
        Connection con = JDBCUtilsConfig.getConnection();
        System.out.println(con);
    }
}

你可能感兴趣的:(数据库)