zoukankan      html  css  js  c++  java
  • 关系管理系统:JdbcUtils工具类代码

    package cn.itcast.utils;
    
    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 JdbcUtils {
        
        private static String url = null;
        private static String username = null;
        private static String password = null;
        
        static{
            try{
                InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
                Properties prop = new Properties();
                prop.load(in);
                
                String driver = prop.getProperty("driver");
                url = prop.getProperty("url");
                username = prop.getProperty("username");
                password = prop.getProperty("password");
                
                Class.forName(driver);
                
            }catch (Exception e) {
                throw new ExceptionInInitializerError(e);
            }
        }
    
        public static Connection getConnection() throws SQLException{
            
            return DriverManager.getConnection(url, username, password);
        }
        
        public static void release(Connection conn,Statement st,ResultSet rs){
            
            
            if(rs!=null){
                try{
                    rs.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
                rs = null;
    
            }
            if(st!=null){
                try{
                    st.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
                
            }
            
            if(conn!=null){
                try{
                    conn.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
                
            }
        }
        
    }

    db.properties配置文件

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/customer
    username=root
    password=root
    
    #Oracle 数据库驱动
    #driver=oracle.jdbc.driver.OracleDriver
    #url=jdbc:oracle:thin:@localhost:1521:orcl
    #username=system
    #password=itcast
  • 相关阅读:
    OpenMP
    linux下编写C++程序播放音频
    Canny Edge Detector
    部署服务器
    第五周--论文泛读
    AI研习社“看图猜作者”优秀代码技术总结
    Neural Multimodal Cooperative Learning Toward Micro-Video Understanding学习笔记
    第二次作业:卷积神经网络 part 2
    循环神经网络
    ORB-SLAM学习笔记
  • 原文地址:https://www.cnblogs.com/lichone2010/p/3175888.html
Copyright © 2011-2022 走看看