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

  • 相关阅读:
    一个简单的ASP.NET MVC异常处理模块
    通过源码了解ASP.NET MVC 几种Filter的执行过程
    跨站请求伪造
    显示实现接口
    数组为什么可以使用linq查询
    编写轻量ajax组件03-实现(附源码)
    事件
    编写轻量ajax组件02-AjaxPro浅析
    委托
    静态构造函数
  • 原文地址:https://www.cnblogs.com/stu-jyj3621/p/14308053.html
Copyright © 2011-2022 走看看