2

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
Brian
  • 7,955
  • 16
  • 66
  • 107

1 Answers1

6

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

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I do have the CLASSPATH set perfectly from the bashrc file. Just to prove it, when I type "echo $CLASSPATH", it echoes the mysql-connector-java-5.1.14-bin.jar directory correctly. And according to the manual, to install the driver, all I need to do is place the directory for the driver JAR in the CLASSPATH. Also, sorry about the typo, I fixed that but still got the exact same error. I've also commented out the newInstance(), but I'm still getting the same stack trace. – Brian Jan 20 '11 at 01:37
  • How exactly are you running the program? Did you understand my answer on your [previous question](http://stackoverflow.com/questions/4720284/jdbc-classpath-not-working/4720328#4720328)? If not, you have to tell in detail how exactly you're running the Java program. – BalusC Jan 20 '11 at 02:11
  • BalusC, I looked at one of the related questions that really provided some detailed ways of setting up JDBC. I did what you said, but now I'm getting an error that doesn't make sense... here it is: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '????????????????' at line 1 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1049) com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)... – Brian Jan 20 '11 at 02:12
  • Well, I'm running it in this way: "java -jar server.jar"... it's worked in the past. I also assume that my CLASSPATH has been correctly configured. Also, when I build the JAR in Eclipse, it also includes the mysql-connector-java-5.1.14.bin.jar in the build path of course. Other than that, I don't understand how else I should execute this. I'm sorry I sound quite amateur at this. :( – Brian Jan 20 '11 at 02:16
  • Thanks a lot, I'm still learning how to use StackOverflow's environmental. It's a pretty nice system I have to admit, lol. – Brian Jan 20 '11 at 02:17
  • As answered in your [previous question](http://stackoverflow.com/questions/4720284/jdbc-classpath-not-working/4720328#4720328), the `CLASSPATH` environment variable is **ignored** when you use `-jar` argument. – BalusC Jan 20 '11 at 02:18
  • Ah ok, thanks for the pointer then. I guess I should use java -cp...? By the way, I'm writing a new question for the ???????????????????? expcetion I'm getting now. Hope you can answer it; you're quite helpful in letting me understand what I'm doing wrong. – Brian Jan 20 '11 at 02:22
  • As stated in the previous answer, when you want to execute it as JAR, the classpath has to be definied in its `MANIFEST.MF` file like `Class-Path: relative/path/to/mysql.jar`. As to your new problem/question, don't forget to include the relevant Java code and SQL query. – BalusC Jan 20 '11 at 02:25
  • Here's the link: http://stackoverflow.com/questions/4742927/stumped-sql-exception-for-jdbc tell me if that's enough relevant Java code. – Brian Jan 20 '11 at 02:29
  • I didn't expect that it came from `getConnection()`. I posted an answer anyway. – BalusC Jan 20 '11 at 02:39