当前位置:   article > 正文

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

java连接mysql

方式1

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

方式2

  1. public void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
  2. Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
  3. Driver driver = (Driver) aClass.newInstance();
  4. String url = "jdbc:mysql://localhost:3306/db02";
  5. Properties properties = new Properties();
  6. properties.setProperty("user", "root");
  7. properties.setProperty("password", "1234");
  8. Connection connect = driver.connect(url, properties);
  9. System.out.println(connect);
  10. }

方式3

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

方式4

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


方式5

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

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号