zoukankan      html  css  js  c++  java
  • JAVA JDBC 连接数据库

    方式一

    Driver driver = new com.mysql.jdbc.Driver();
    String url = "jdbc:mysql://localhost:3306/test";
    Properties info = new Properties();
    info.setProperty("user","root");
    info.setProperty("password","1234");
    Connection connection = driver.connect(url,info);
    System.out.println(connection);

     方式二 使用反射

    Class cl = Class.forName("com.mysql.jdbc.Driver");
    
    Driver driver = (Driver) cl.newInstance();
    String url = "jdbc:mysql://localhost:3306/test";
    Properties info = new Properties();
    info.setProperty("user","root");
    info.setProperty("password","1234");
    Connection connection = driver.connect(url,info);
    System.out.println(connection);

     方式三 使用Drivermanager

            //利用反射
            Class cl = Class.forName("com.mysql.jdbc.Driver");
    
            Driver driver = (Driver) cl.newInstance();
            String url = "jdbc:mysql://localhost:3306/test";
            String user = "root";
            String password="1234";
            DriverManager.registerDriver(driver);
            Connection connection = DriverManager.getConnection(url,user,password);
            System.out.println(connection);    

     方式4 可以省略显示的驱动加载

            String url = "jdbc:mysql://localhost:3306/test";
            String user = "root";
            String password="1234";
            //利用反射
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager.getConnection(url,user,password);
            System.out.println(connection);

    方式5 通过配置文件加载配置信息

            InputStream is = connectTest.class.getClassLoader().getResourceAsStream("jdbcInfo.properties");
            Properties pro = new Properties();
            pro.load(is);
    
            String user = pro.getProperty("user");
            String password = pro.getProperty("password");
            String url = pro.getProperty("url");
            String driverClass = pro.getProperty("driverClass");
            //利用反射
            Class.forName(driverClass);
            Connection connection = DriverManager.getConnection(url,user,password);
            System.out.println(connection);
  • 相关阅读:
    Microsoft Azure 的负载平衡器的Session Sticky
    MySQL Performance tuning
    集装箱码头智能理货方案
    Iphone 英语语言下通讯录排序问题
    mac安装Aws cli失败
    vbs操作excel
    Sharepoint 2010 工作流启动时处理出错
    2、easyUI-创建 CRUD可编辑dataGrid(表格)
    1、easyUI-创建 CRUD普通dataGrid(表格)
    0327定时执行--存储过程--dbms_job--dbms_scheduler.create_job
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/12372837.html
Copyright © 2011-2022 走看看