java 连接mysql数据库方式汇总

jdbc:
简单示例
方法四:直接连class都不用写,这样得出的东西是什么很好奇就是只是不获取driver?
看源码后发现:

package java.sql.DriverManager;

Applications no longer need to explicitly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work withoutmodification.

所以最简单的方式为:
(p.getProperty(“user”)直接用user也行)
Connection conn = DriverManager.getConnection(url,p.getProperty(“user”),p.getProperty(“password”));
(当然可以的话还是加上Driver driver = Class.forName(com.mysql.cj.jdbc.Driver)毕竟免得有其他bug,因为他是从其他地方加载的。)

When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly using the same classloader as the current applet or application.

后续待补充:

你可能感兴趣的:(数据库,mysql,java)