JDBC 4.0 开始Java操作数据库不用再使用 Class.forName加载驱动类了
目录代码示例
转自 https://docs.oracle.com/javase/tutorial/jdbc/overview/index.html
public void connectToAndQueryDatabase(String username, String password) {
// DriverManager.loadInitialDrivers() 通过 SPI 机制加载驱动类,不需再手动写 Class.forName("*.*.DriverClass")
Connection con = DriverManager.getConnection(
"jdbc:myDriver:myDatabase",
username,
password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) {
int x = rs.getInt("a");
String s = rs.getString("b");
float f = rs.getFloat("c");
}
}