当前位置:   article > 正文

jdbc mysql url写法_jdbc获取数据库连接的五种方式

url=jdbc:mysql
  1. package com.atguigu.connection;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.sql.Connection;
  5. import java.sql.Driver;
  6. import java.sql.DriverManager;
  7. import java.sql.SQLException;
  8. import java.util.Properties;
  9. import org.junit.Test;
  10. public class ConnectionTest {
  11. //方式一:
  12. @Test
  13. public void testConnection1() throws SQLException {
  14. //获取Driver实现类对象
  15. Driver driver = new com.mysql.jdbc.Driver();
  16. //jdbc:mysql:协议
  17. //localhost:ip地址
  18. //3306:默认mysql的端口号
  19. //hutubill自己的数据库,根据自己数据库名字填写
  20. String url = "jdbc:mysql://localhost:3306/hutubill";
  21. //将用户名和密码封装在properties中
  22. Properties info = new Properties();
  23. info.setProperty("user", "root");
  24. info.setProperty("password", "admin");
  25. Connection conn = driver.connect(url , info );
  26. System.out.println(conn);
  27. }
  28. //方式二:对方式一的迭代:在如下的程序中不出现第三方api,使得程序具有更好的移植性
  29. @Test
  30. public void testConnection2() throws Exception {
  31. //获取Driver实现类对象,使用反射
  32. Class clazz = Class.forName("com.mysql.jdbc.Driver");
  33. @SuppressWarnings("deprecation")
  34. Driver driver = (Driver) clazz.newInstance();
  35. //提供要连接的数据库
  36. String url = "jdbc:mysql://localhost:3306/hutubill";
  37. //提供链接需要的用户名和密码(自己的)
  38. Properties info = new Properties();
  39. info.setProperty("user", "root");
  40. info.setProperty("password", "admin");
  41. //获取连接
  42. Connection conn = driver.connect(url, info);
  43. System.out.println(conn);
  44. }
  45. //方式三:使用DriverManager(具体的类)来替换Driver
  46. @Test
  47. public void testConnection3() throws Exception {
  48. //1.获取Driver实现类的对象
  49. Class clazz = Class.forName("com.mysql.jdbc.Driver");
  50. @SuppressWarnings("deprecation")
  51. Driver driver = (Driver) clazz.newInstance();
  52. //2.提供另外三个连接的基本信息
  53. String url = "jdbc:mysql://localhost:3306/hutubill";
  54. String user = "root";
  55. String password = "admin";
  56. //3.注册驱动
  57. DriverManager.registerDriver(driver);
  58. //获取连接
  59. Connection conn = DriverManager.getConnection(url, user, password);
  60. System.out.println(conn);
  61. }
  62. //方式四:对方式三的优化
  63. @Test
  64. public void testConnection4() throws Exception {
  65. //1.提供另外三个连接的基本信息
  66. String url = "jdbc:mysql://localhost:3306/hutubill";
  67. String user = "root";
  68. String password = "admin";
  69. //2.加载Driver
  70. Class.forName("com.mysql.jdbc.Driver");
  71. //相较于方式三,可以省略如下操作:
  72. // @SuppressWarnings("deprecation")
  73. // Driver driver = (Driver) clazz.newInstance();
  74. // //注册驱动
  75. // DriverManager.registerDriver(driver);
  76. //为什么可以省略上述操作的?
  77. /**
  78. * 在mysql的Driver实现类中,声明了如下的操作:
  79. * static{
  80. * try{
  81. * java.sql.DriverManager.registerDriver(new Driver());
  82. * }catch (SQLException E){
  83. * throw new RuntimeException("Can't register drvier!");
  84. * }
  85. */
  86. //3.获取连接
  87. Connection conn = DriverManager.getConnection(url, user, password);
  88. System.out.println(conn);
  89. }
  90. //方式五:最终版 将数据库连接需要的四个基本信息声明在配置文件中,通过读取配置文件的方式,获取连接
  91. /**
  92. * 1.实现了数据与代码的分离,实现了解耦。
  93. * 2.修改配置文件信息,可以避免程序重新打包。
  94. * @throws Exception
  95. */
  96. @Test
  97. public void getConnection5() throws Exception {
  98. //1.读取配置文件信息
  99. InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
  100. Properties pros = new Properties();
  101. pros.load(is);
  102. String user = pros.getProperty("user");
  103. String password = pros.getProperty("password");
  104. String url = pros.getProperty("url");
  105. String driverClass = pros.getProperty("driverClass");
  106. //2.加载驱动
  107. Class.forName(driverClass);
  108. //3.获取连接
  109. Connection conn = DriverManager.getConnection(url, user, password);
  110. System.out.println(conn);
  111. }
  112. }

ac20df9d3869fd47d3278c50f186a1a2.png

8478935ae2fd4ffa0aca921a4ee45f84.png

user=root

password=admin

url=jdbc:mysql://localhost:3306/hutubill

driverClass=com.mysql.jdbc.Driver

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

闽ICP备14008679号