JDBC Type 4 Driver
According to the MySQL 5.0 Reference Manual, the MySQL JDCB Connector/J is a "Type 4" driver. The method I'm using to connect to the JDBC server is...
Class.forName("com.mysql.jdbc.Driver").newInstance();
My JDBC simply won't work, and as part of my last resort to find out why this wouldn't work, I think it could be the driver. Could someone tell me if this is the correct way to call the driver for a getConnection()? What I'm really looking for 开发者_运维百科is a different command that should replace "com.mysql.jdbc.Driver" for Type 4 JDBC drivers.
Here's the stack trace that led me to think why this isn't working...
java.sql.SQLException: No driver found for jdbc:mysql://localhost:3306/mysql?user=user_name&password=pass_word
java.sql.DriverManager.getDriver(libgcj.so.10)
java.sql.DriverManager.getConnection(libgcj.so.10)
java.sql.DriverManager.getConnection(libgcj.so.10)
... blah blah blah
java.sql.SQLException: No driver found for jdbc:mysql//localhost:3306/mysql?user=user_name&password=pass_word
This means that either the JDBC driver is missing in the runtime classpath (and you suppressed the ClassNotFoundException
and continued on), or that the JDBC URL is wrong. There's actually an error in your JDBC URL, the :
is missing between mysql
and //
. It should look like
jdbc:mysql://localhost:3306/mysql?user=user_name&password=pass_word
This has nothing to do with the driver type. Note that the newInstance()
call is unnecessary on the com.mysql.jdbc.Driver
. This is a leftover of a bug in its ancient predecesor org.gjt.mm.mysql.Driver
which registered itself with DriverManager
in the constructor instead of in a static initializer block.
Related questions
- Java connectivity with MySQL
- Difference between JDBC driver type numbers
精彩评论