zoukankan      html  css  js  c++  java
  • JDBC 2—— 获取数据库连接

     JDBCTest01.class

    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;
    
    import com.mysql.jdbc.Driver;
    
    public class JDBCTest01 {
        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, IOException {
            testConnection5();
        }
    
        //方式一:
        public static void testConnection1() throws SQLException {
            // 1、注册驱动
            Driver driver = new com.mysql.jdbc.Driver();
            // url:http//localhost:8080/gmall/keyboard.jpg
            // jdbc:mysql:协议
            // 3360:默认mysql的端口号
            // test:test数据库
            String url = "jdbc:mysql://localhost:3306/MySQL";
            Properties info = new Properties();
            // 将用户名和密码封装在Propect中
            info.setProperty("user", "root");
            info.setProperty("password", "root");
            Connection coon = driver.connect(url, info);
            System.out.println(coon);
        }
        
        //方式二:对方式一的迭代
        public static void testConnection2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
            //1、获取Driver实现类对象,使用反射
            Class<?> clazz = Class.forName("com.mysql.jdbc.Driver");
            @SuppressWarnings("deprecation")
            Driver driver = (Driver) clazz.newInstance();
            
            //2、提供要连接的数据库
            String url = "jdbc:mysql://localhost:3306/MySQL";
            
            //3、提供连接需要的用户名和密码
            Properties info = new Properties();
            info.setProperty("user", "root");
            info.setProperty("password", "root");
            
            //4、获取连接
            Connection coon = driver.connect(url, info);
            System.out.println(coon);
        }
        
        //方式三:使用DriverManger替换Driver
        public void testConnection3() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
            //1、获取Driver实现类对象,使用反射
            Class<?> clazz = Class.forName("com.mysql.jdbc.Driver");
            @SuppressWarnings("deprecation")
            Driver driver = (Driver) clazz.newInstance();
            
            //2、提供要连接的数据库
            String url = "jdbc:mysql://localhost:3306/MySQL";
            String user = "root";
            String password = "root";
            
            //3、注册驱动
            DriverManger.registerDriver(driver);
            
            //4、获取连接
            Connection coon = DriverManager.getConnection(url, user, password);
            System.out.println(coon);
        }
        
        //方式四:优化三
        public static void testConnection4() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
            //1、提供要连接的数据库
            String url = "jdbc:mysql://localhost:3306/MySQL";
            String user = "root";
            String password = "root";
            
            //2、加载Driver
            Class.forName("com.mysql.jdbc.Driver");
            @SuppressWarnings("deprecation")
    //        Driver driver = (Driver) clazz.newInstance();
    //        //注册驱动
    //        DriverManger.registerDriver(driver);
            //为什么可以省略上述操作呢:
            /*
             * 在mysql的Driver实现类中,申明的如下操作:
             *     static{
             *         try{
             *             java.sql.DriverManager.registerDriver(new Driver());
             *         }catch (SQLException E) {
             *             throw new RuntimeException("Can't register driver!");
             *         }
             *     }
             * */
            
            //3、获取连接
            Connection coon = DriverManager.getConnection(url, user, password);
            System.out.println(coon);
        }
        
        //方式五(final版):将数据库连接需要的4个基本信息声明在配置文件中,通过读取配置文件的方式,获取连接
        /*
         * 此种方式的好处?
         * 1、实现了数据与代码的分离,实现了解耦
         * 2、如果需要修改配置文件信息,可以避免程序重新打包
         * */
        public static void testConnection5() throws IOException, ClassNotFoundException, SQLException {
            //1、读取配置文件中的4个基本信息
            InputStream is = JDBCTest01.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties pros = new Properties();
            pros.load(is);
            
            String user = pros.getProperty("user");
            String password = pros.getProperty("password");
            String url = pros.getProperty("url");
            String driverClass = pros.getProperty("driverClass");
            
            //2、加载驱动
            Class.forName(driverClass);
            
            //3、获取连接
            Connection coon = DriverManager.getConnection(url, user, password);
            System.out.println(coon);
        }
    }

    jdbc.properties

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

  • 相关阅读:
    SQL Server 存储过程
    Prettier
    VSCode常用插件之open in browser使用
    VSCode常用插件之vscode-fileheader使用
    (四)Vue面试题解析,目标高级前端开发者必经之路
    (三)Vue必考面试题解析,继续向高级前端迈进
    (二)Vue常见面试题,向高级前端开发迈进
    (一)Vue常见面试题,看看你都会了吗?
    Vue2.x 开发必须知道的 36 个技巧
    开箱即用 vue-cli4 vant rem 移动端框架方案
  • 原文地址:https://www.cnblogs.com/stu-jyj3621/p/14308053.html
Copyright © 2011-2022 走看看