• Java—JBDC(通过properties配置文件链接数据库)


    public class JBDC_V1 {
        private static String driver;
        private static String url;
        private static String username;
        private static String password;
    /**
     * 静态代码块加载配置文件信息
     */
    
        static {
            try{
               ResourceBundle bundle = ResourceBundle.getBundle("db");
               driver = bundle.getString("driver");
               url = bundle.getString("url");
               username = bundle.getString("username");
               password = bundle.getString("password");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    
        public static Connection getConnection() {
            Connection conn = null;
            try {
                Class.forName(driver);
                conn = DriverManager.getConnection(url, username, password);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return conn;
        }
    
    
        public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
    
        }
    }

    public class JBDC_V2 {
        private static String driver;
        private static String url;
        private static String username;
        private static String password;
    
        /**
         * 静态代码块加载配置文件信息
         */
        static {
            try {
                // 1.通过当前类获取类加载器
                ClassLoader classLoader = JBDC_V2.class.getClassLoader();
                // 2.通过类加载器的方法获得一个输入流
                InputStream is = classLoader.getResourceAsStream("db.properties");
                // 3.创建一个properties对象
                Properties props = new Properties();
                // 4.加载输入流
                props.load(is);
                // 5.获取相关参数的值
                driver = props.getProperty("driver");
                url = props.getProperty("url");
                username = props.getProperty("username");
                password = props.getProperty("password");
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
        /**
         * 获取连接方法
         *
         * @return
         */
        public static Connection getConnection() {
            Connection conn = null;
            try {
                Class.forName(driver);
                conn = DriverManager.getConnection(url, username, password);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return conn;
        }
    
        public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    配置文件(放置src目录)

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf8
    username=root
    password=

    检测类

    public class JBDC_Test {

    @Test
    public void test2() {
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
    // 1.获取连接
    conn = JBDC_V2.getConnection();
    // 2.编写sql语句
    String sql = "insert t1 (id,name) value (?,?)";
    // 3.获取执行sql语句对象
    pstmt = conn.prepareStatement(sql);
    // 4.设置参数
    pstmt.setInt(1, 5);
    pstmt.setString(2, "xjj");
    // 5.执行删除操作
    int row = pstmt.executeUpdate();
    if (row > 0) {
    System.out.println("删除成功!");
    } else {
    System.out.println("删除失败!");
    }
    } catch (Exception e) {
    throw new RuntimeException(e);
    } finally {
    // 6.释放资源
    JBDC_V2.release(conn, pstmt, null);
    }
    }
    public class JBDC_V2 {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    /**
    * 静态代码块加载配置文件信息
    */
    static {
    try {
    // 1.通过当前类获取类加载器
    ClassLoader classLoader = JBDC_V2.class.getClassLoader();
    // 2.通过类加载器的方法获得一个输入流
    InputStream is = classLoader.getResourceAsStream("db.properties");
    // 3.创建一个properties对象
    Properties props = new Properties();
    // 4.加载输入流
    props.load(is);
    // 5.获取相关参数的值
    driver = props.getProperty("driver");
    url = props.getProperty("url");
    username = props.getProperty("username");
    password = props.getProperty("password");
    } catch (IOException e) {
    e.printStackTrace();
    }

    }

    /**
    * 获取连接方法
    *
    * @return
    */
    public static Connection getConnection() {
    Connection conn = null;
    try {
    Class.forName(driver);
    conn = DriverManager.getConnection(url, username, password);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return conn;
    }

    public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
    if (rs != null) {
    try {
    rs.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (pstmt != null) {
    try {
    pstmt.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (conn != null) {
    try {
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    }
    }
  • 相关阅读:
    oracle 分页sql
    Oracle 11g数据库详细安装步骤图解
    轻量级ORM框架——第二篇:Dapper中的一些复杂操作和inner join应该注意的坑
    轻量级ORM框架——第一篇:Dapper快速学习
    MY SQL 两种安装方式
    在本地库不连接远远程库的情况下操作远程库-----sql server
    学习Java泛型(记得通配符)
    学习java泛型(还是集合)
    学习java011902迭代器
    学习Java-继续集合
  • 原文地址:https://www.cnblogs.com/zhuzhiwei-2019/p/11300565.html
Copyright © 2020-2023  润新知