学校时,照抄老师的代码。工作时,直接调用成熟框架的方法。从没有真正自己研究下。(惭愧)
今天试着自己写写JDBC连接数据库。
看到代码
……
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection(url,username,password);
……
我在想孤零零的一句【Class.forName("org.gjt.mm.mysql.Driver");】到底做了什么,网友说:注册MySql驱动。可是自己习惯思维是,看到调用。这连引用都没有。它加载了谁,又调用了谁呢?
想不出来!“何不看看驱动源码”呢?对啊!理解解压【org.gjt.mm.mysql.Driver】
org\gjt\mm\mysql文件夹下就一个文件:类Driver
package org.gjt.mm.mysql;
import java.sql.SQLException;
public class Driver extends com.mysql.jdbc.Driver
{
}
再去看看 com.mysql.jdbc.Driver
package com.mysql.jdbc;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Driver extends NonRegisteringDriver
implements java.sql.Driver
{
static
{
try
{
DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
}
兜兜转转,最后调用的还是【DriverManager.registerDriver(new Driver());】
但源文件里的
public static synchronized void registerDriver(java.sql.Driver driver)
throws SQLException {
if (!initialized) {
initialize();
}
DriverInfo di = new DriverInfo();
di.driver = driver;
di.driverClass = driver.getClass();
di.driverClassName = di.driverClass.getName();
// Not Required -- drivers.addElement(di);
writeDrivers.addElement(di);
println("registerDriver: " + di);
/* update the read copy of drivers vector */
readDrivers = (java.util.Vector) writeDrivers.clone();
}
就不是十分理解了!
分享到:
相关推荐
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); // 或者 Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&...
mysql4.1.10的JDBC驱动 把它拷贝到你的web应用的WEB-INF\lib下(当然也可以设置CLASSPATH) <br>接着修改程序代码: 将你的: Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 改为: ...
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url = "jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"; Connection conn = ...
2. 驱动注册:在Java应用程序中,你需要通过Class.forName()方法注册对应的驱动类,例如"com.mysql.jdbc.Driver"(对于5.x)或"com.mysql.cj.jdbc.Driver"(对于8.x)。 3. 连接URL:连接URL会根据MySQL服务器的...
2. 使用`Class.forName()`加载驱动:`Class.forName("org.gjt.mm.mysql.Driver");` 3. 调用`DriverManager.getConnection()`建立连接:`cn=DriverManager.getConnection("jdbc:mysql://MyDbComputerNameOrIP:3306/...
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url = "jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"; Connection conn = DriverManager...
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url = "jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"; Connection conn = ...
Class.forName("org.gjt.mm.mysql.Driver"); String url = "jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"; Connection conn = DriverManager.getConnection...
Class.forName("org.gjt.mm.mysql.Driver"); Connection cn = DriverManager.getConnection( "jdbc:mysql://MyDbComputerNameOrIP:3306/myDatabaseName", sUsr, sPwd ); ``` 2. **PostgreSQL** 对于...
Class.forName("org.gjt.mm.mysql.Driver"); cn = DriverManager.getConnection("jdbc:mysql://DBServerIP:3306/myDatabaseName", sUsr, sPwd); ``` 五、JDBC 连接 PostgreSQL PostgreSQL 是一种功能强大且开源的...
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url = "jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"; Connection conn = ...
在Jsp文件中,可以直接使用`Class.forName("org.gjt.mm.mysql.Driver")`来加载驱动,然后通过`java.sql.Connection`接口建立数据库连接。 **Java连接Mysql数据库** 在Java程序中,同样需要引入Mysql JDBC驱动。在...
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); // 或者 Class.forName("com.mysql.jdbc.Driver") String url = "jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&...
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url = "jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"; Connection conn = ...
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url = "jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"; Connection conn = ...
Class.forName("org.gjt.mm.mysql.Driver"); String url = "jdbc:mysql://localhost/testDB?user=testuser&password=testpassword&useUnicode=true&characterEncoding=GB2312"; Connection conn = DriverManager.get...
Class.forName("org.gjt.mm.mysql.Driver"); Connection cn = DriverManager.getConnection("jdbc:mysql://MyDbComputerNameOrIP:3306/myDatabaseName", sUsr, sPwd); ``` 这里的URL格式是`jdbc:mysql://主机名:端口...
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"; Connection conn= DriverManager....
Class.forName("org.gjt.mm.mysql.Driver"); String url = "jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"; Connection conn = DriverManager.getConnection...
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url = "jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"; Connection conn = ...