JdbcUtil工具类并解析结果集

原生jdbc连接数据库
import java.sql.*;

public class JDBCUtil {
    private static String driver ="com.mysql.cj.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
    private static String username = "root";
    private static String password = "123456";

    /**
     * 只注册一次,静态代码块
     */
    static{
        //注册驱动程序
        try {
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 释放资源的方法
     */
    public static void close(ResultSet rs, Statement stmt, Connection conn){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }
}
调用并解析结果集封装成List
Connection connection = JdbcDemo.getConnection();
List<Map<String, String>> returnVal = new ArrayList<>();
try {
    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("select * from user");
    while (resultSet.next()) {
        //将ResultSet封装成List>结构的集合
        HashMap<String, String> map = new HashMap<>();
        String id = resultSet.getString("id");
        String name = resultSet.getString("name");
        map.put("id", id);
        map.put("name", name);
        returnVal.add(map);
    }
    JdbcDemo.close(resultSet,statement,connection);
} catch (SQLException e) {
    e.printStackTrace();
}
通过读取配置文件获取连接信息两种方式
ResourceBundle jdbcproperties = ResourceBundle.getBundle("jdbcproperties");
System.out.println(jdbcproperties.getString("driver"));
System.out.println(jdbcproperties.getString("url"));
System.out.println(jdbcproperties.getString("username"));
System.out.println(jdbcproperties.getString("password"));

File file = new File("绝对路径\\jdbcproperties.properties");
InputStream fileInputStream = new FileInputStream(file);
Properties properties = new Properties();
properties.load(fileInputStream);
System.out.println(properties.get("driver"));
System.out.println(properties.get("username"));
System.out.println(properties.get("password"));
System.out.println(properties.get("url"));
用第一种获取配置信息的方式Jdbc工具类代码修改如下:
static{
  ResourceBundle jdbcproperties = ResourceBundle.getBundle("jdbcproperties");
    driver = jdbcproperties.getString("driver");
    url = jdbcproperties.getString("url");
    username = jdbcproperties.getString("username");
    password = jdbcproperties.getString("password");
    //注册驱动程序
    try {
        Class.forName(driver);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public static Connection getConnection(){
    Connection conn = null;
    try {
        conn = DriverManager.getConnection(url,username,password);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return conn;
}

你可能感兴趣的:(JAVA)