zoukankan      html  css  js  c++  java
  • JAVA jdbc获取数据库连接

    JDBC获取数据库连接的帮助类

    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class jdbcUtil {
    
        private static String driver;
        private static String url;
        private static String id;
        private static String password;
    
        //加载文件,获取配置参数
        static{
            Properties prop=new Properties();
            InputStream is;
            try {
                is = jdbcUtil.class.getClassLoader()
                    .getResourceAsStream("com/sy/db/db.properties");
                prop.load(is);
                driver = prop.getProperty("driver");
                url = prop.getProperty("url");
                id = prop.getProperty("id");
                password = prop.getProperty("password");
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    
        //注册驱动
        static{
            try {
                Class.forName(driver);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        //取得连接
        public static Connection getMysqlConnection(){
            Connection conn=null;
            try {
                conn=DriverManager.getConnection(url,id,password);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return conn;
        }
    
        //关闭流
        public static void close(ResultSet rs){
            if(null!=rs){
                try {
                    rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        public static void close((Statement state){
            if(null!=rs){
                try {
                    state.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        public static void close(Connection conn){
            if(null!=rs){
                try {
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 相关阅读:
    CentOS 8.0配置阿里云yum源和epel源
    CentOS8 安装epel 使用阿里云镜像
    centos下yum使用proxy代理方法
    MySQL中的事务控制(一)start transaction
    MySQL中的锁定语句: lock tables 和 unlock tables
    MySQL中的触发器
    MySQL中的事件调度器
    MySQL中的流程控制
    MySQL中的不可见索引、倒序索引
    IDEA出现Push to origin/master was rejected
  • 原文地址:https://www.cnblogs.com/sweetyu/p/4942079.html
Copyright © 2011-2022 走看看