zoukankan      html  css  js  c++  java
  • 标准获得连接和关闭连接模板

    模板一:

    package js;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class JdbcUtil {
        private static String url="jdbc:mysql://localhost:3306/day01";
        private static String user="root";
        private static String password="root";
        
        static{
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                throw new RuntimeException();
            }
        }
        
        /*
         * 获取连接
         */
        public static Connection getConnection(){
            try {
                Connection conn= DriverManager.getConnection(url, user, password);
                return conn;
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }    
        }
        
        public static void close(Connection conn,Statement smt,ResultSet rs){
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
            if(smt!=null){
                try {
                    smt.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);
                }
            }
        }
    }

     在配置文件中读取信息:

    db.properties:

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

    模板二:

    package js;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    
    
    public class JdbcUtil {
        private static String url=null;
        private static String user=null;
        private static String password=null;
        private static String driverClass=null;
        static{
            
            try {
                /*
                 * 使用类路径的读取方式:
                 *   / :斜杠标示classpath的根目录
                 *      在java项目下:classpath的根目录从bin开始
                 *      在web 项目下:classpath的根目录从WEB-INF/classes目录开始
                 */
                //读取db.properties文件
                Properties props=new Properties();
                InputStream in= JdbcUtil.class.getResourceAsStream("/db.properties");
                props.load(in);
                url=(String) props.get("url");
                user=props.getProperty("user");
                password=props.getProperty("password");
                driverClass=props.getProperty("driverClass");
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                Class.forName(driverClass);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                throw new RuntimeException();
            }
        }
        
        /*
         * 获取连接
         */
        public static Connection getConnection(){
            try {
                Connection conn= DriverManager.getConnection(url, user, password);
                return conn;
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }    
        }
        /*
         * 关闭连接
         */
        public static void close(Connection conn,Statement smt,ResultSet rs){
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
            if(smt!=null){
                try {
                    smt.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);
                }
            }
        }
    }
  • 相关阅读:
    课堂作业04 2017.10.27
    课程作业 03 动手动脑 2017.10.20
    课程作业 03 2017.10.20
    HDU 3974 Assign the task
    POJ 2155 Matrix
    POJ 2481 Cows
    HDU 3038 How Many Answers Are Wrong
    CS Academy Array Removal
    POJ_1330 Nearest Common Ancestors LCA
    CF Round 427 D. Palindromic characteristics
  • 原文地址:https://www.cnblogs.com/lyjs/p/5019506.html
Copyright © 2011-2022 走看看