当前位置:   article > 正文

Java连接MySQL数据库的五种方式,jdbc

java连接mysql
  1. package day38;
  2. import com.mysql.jdbc.Driver;
  3. import org.junit.jupiter.api.Test;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.SQLException;
  9. import java.util.Properties;
  10. /**
  11. * @Author:monian
  12. * @Wo yi wu ta,wei shou shu er!
  13. * @Date:2023/11/14 12:09
  14. */
  15. public class JdbcConn {
  16. @SuppressWarnings({"all"})
  17. @Test
  18. public void connect01() throws SQLException {
  19. // 方式1
  20. Driver driver = new Driver();
  21. String url = "jdbc:mysql://localhost:3306/mn_db02";
  22. // 将用户名和密码放入到Properties对象
  23. Properties properties = new Properties();
  24. properties.setProperty("user", "root");
  25. properties.setProperty("password", "chen");
  26. Connection connect = driver.connect(url, properties);
  27. System.out.println("方式1" + connect);
  28. }
  29. @Test
  30. //方式2
  31. public void connect02() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
  32. //使用反射加载Driver类
  33. Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
  34. Driver driver = (Driver) aClass.newInstance();
  35. String url = "jdbc:mysql://localhost:3306/mn_db02";
  36. // 将用户名和密码放入到Properties对象
  37. Properties properties = new Properties();
  38. properties.setProperty("user", "root");
  39. properties.setProperty("password", "chen");
  40. Connection connect = driver.connect(url, properties);
  41. System.out.println("方式2=" + connect);
  42. }
  43. @Test
  44. //方式3 使用DriverManager 替代 Driver 进行统一管理
  45. public void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
  46. Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
  47. Driver driver = (Driver) aClass.newInstance();
  48. //创建url 和 user 和 password
  49. String url = "jdbc:mysql://localhost:3306/mn_db02";
  50. String user = "root";
  51. String password = "chen";
  52. DriverManager.registerDriver(driver);
  53. Connection connection = DriverManager.getConnection(url, user, password);
  54. System.out.println("方式3=" + connection);
  55. }
  56. @Test
  57. /**方式4: 使用Class.forName 自动完成注册驱动,简化代码
  58. * 推荐指数:*****
  59. */
  60. public void connect04() throws ClassNotFoundException, SQLException {
  61. //使用反射加载了Driver
  62. //再加载Driver类时,完成注册
  63. /*
  64. 源码:1.静态代码块,在类加载一次,会执行一次
  65. 2.DriverManager.registerDriver(new Driver());
  66. 3.因此注册driver的工作已经完成
  67. static {
  68. try {
  69. DriverManager.registerDriver(new Driver());
  70. } catch (SQLException var1) {
  71. throw new RuntimeException("Can't register driver!");
  72. }
  73. }
  74. */
  75. Class.forName("com.mysql.jdbc.Driver"); // 删除也可以。建议写上
  76. //创建url 和 user 和 password
  77. String url = "jdbc:mysql://localhost:3306/mn_db02";
  78. String user = "root";
  79. String password = "chen";
  80. Connection connection = DriverManager.getConnection(url, user, password);
  81. System.out.println("方式4~" + connection);
  82. }
  83. @Test
  84. //推荐指数:****** 代码简洁+反射机制
  85. //方式5 ,在方式4的基础上改进,增加配置文件,让连接mysql更加灵活
  86. public void connect05() throws IOException, ClassNotFoundException, SQLException {
  87. //通过Properties对象获取配置文件的信息
  88. Properties properties = new Properties();
  89. properties.load(new FileInputStream("src\\mysql.properties"));
  90. String user = properties.getProperty("user");
  91. String password = properties.getProperty("password");
  92. String url = properties.getProperty("url");
  93. String driver = properties.getProperty("driver");
  94. Class.forName(driver);//建议写上
  95. Connection connection = DriverManager.getConnection(url, user, password);
  96. System.out.println("方式5=" + connection);
  97. }
  98. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/698516
推荐阅读
相关标签
  

闽ICP备14008679号