小工具知识网

当前位置: 首页 > 行业知识 > Java:连接mysql数据库的五种方式

行业知识

Java:连接mysql数据库的五种方式

2026-10-28 04:20:00 来源:互联网转载

方式1

public void connect01() throws SQLException {        Driver driver = new Driver();        String url = "jdbc:mysql://localhost:3306/db02";        Properties properties = new Properties();        properties.setProperty("user", "root");        properties.setProperty("password", "1234");        Connection connect = driver.connect(url, properties);        System.out.println(connect);    }

方式2

public void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {        Class aClass = Class.forName("com.mysql.jdbc.Driver");        Driver driver = (Driver) aClass.newInstance();        String url = "jdbc:mysql://localhost:3306/db02";        Properties properties = new Properties();        properties.setProperty("user", "root");        properties.setProperty("password", "1234");        Connection connect = driver.connect(url, properties);        System.out.println(connect);    }

方式3

//方式3 使用DriverManager 替代 Diver 进行统一管理 扩展性更好一点    public void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {        //        Class aClass = Class.forName("com.mysql.jdbc.Driver");        Driver driver = (Driver) aClass.newInstance();        String url = "jdbc:mysql://localhost:3306/db02";        String user = "root";        String password = "1234";        DriverManager.registerDriver(driver);//注册Driver驱动        Connection connection = DriverManager.getConnection(url, user, password);        System.out.println(connection);    }

方式4

//方式4 使用Class.forName 自动完成注册驱动 简化代码    //这种方式获取连接是使用最多的,推荐使用    public void connect04() throws ClassNotFoundException, SQLException {        //使用反射加载 Driver类        //在加载 Driver类时 完成注册        /*        * 源码 1. 静态代码块 在类加载的时候 会执行一次        *       2.DriverManager.registerDriver(new Driver());        *       3.因此注册Driver的工作已经完成        *     static{        *             try{        *                  DriverManager.registerDriver(new Driver());        *                }catch(SQLException var1){        *                  throw new RuntimeException("Can't register driver!");        *                }        *               }        *            }        * */        Class.forName("com.mysql.jdbc.Driver");        //1.mysql驱动5.1.6可以无需Class.forName("com.mysql.jdbc.Driver")        //2.从jdk1.5以后使用了jdbc4 不再需要显示调用class.forName()注册驱动而是自动调用驱动        //    jar包下META-INF/services/java.sql.Driver文本中的类名称去注册        //3.建议还是写上Class.forName("com.mysql.jdbc.Driver"),更加明确        //创建url 和 user 和 password        String url = "jdbc:mysql://localhost:3306/db02";        String user = "root";        String password = "1234";        Connection connection = DriverManager.getConnection(url, user, password);        System.out.println(connection);    }

方式5

//方式5 在方式4的基础上改进 增加配置文件 让连接mysql更加灵活    public void connect05() throws IOException, ClassNotFoundException, SQLException {        //通过Properties对象获取配置文件的信息        Properties properties = new Properties();        properties.load(new FileInputStream("src/mysql.properties"));        //获取相关的值        String user = properties.getProperty("user");        String password = properties.getProperty("password");        String driver = properties.getProperty("driver");        String url = properties.getProperty("url");        Class.forName(driver);        Connection connection = DriverManager.getConnection(url, user, password);        System.out.println(connection);    }

上一篇:怎么将服务器开到网易云?

下一篇:Nagios核心安装步骤详解,轻松搭建监控体系,成为运维高手!