Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Help Establishing a Connection with SQL

 
Charles Sexton
Ranch Hand
Posts: 272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need help establishing a connection with MySQL. I have added MySQL connector/j to the jar file. When I run this code I receive this error: java.lang.ClassNotFoundException: java.sql.driver

I have watched lots of youtube videos and all of them seem to be irrelevant.


 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Class.forName() tries to load the class mentioned as argument.
This ClassNotFoundException states that 'java.sql.driver' could not be loaded, as in the class could not be located from the CLASSPATH.
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Charles Sexton:
The MySQL JDBC driver class is named "com.mysql.jdbc.Driver" and that's the class you should be loading.
"java.sql.Driver" is just an interface that should be implemented by all JDBC drivers.

Having said that, JDBC supports a concept of auto discovery of drivers. That means you don't really need any Class.forName() call at all.
Just make sure the MySQL Connector JAR file is in your classpath, and it'll work fine without a Class.forName().
 
Charles Sexton
Ranch Hand
Posts: 272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took out Class.forName(); I receive a new error message.

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
BUILD SUCCESSFUL (total time: 0 seconds)

 
Rob Spoor
Sheriff
Pie
Posts: 20751
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a MySQL server running on your own machine?
 
Charles Sexton
Ranch Hand
Posts: 272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Do you have a MySQL server running on your own machine?


Yes I do, MySQL workbench and MySQL Server 5.6
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jdbc:mysql://3306/localhost/mysqlproject

That doesn't look like a correct MySQL JDBC URL: http://dev.mysql.com/doc/refman/5.6/en/connector-j-reference-configuration-properties.html
 
Charles Sexton
Ranch Hand
Posts: 272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
jdbc:mysql://3306/localhost/mysqlproject

That doesn't look like a correct MySQL JDBC URL: http://dev.mysql.com/doc/refman/5.6/en/connector-j-reference-configuration-properties.html


That is a lot of useful information, Thank You.

Will read the document fully and try a few things and see what I come up with.
 
Charles Sexton
Ranch Hand
Posts: 272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:927)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4689)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1304)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2486)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2519)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2304)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:243)
at online.payment.system.OnlinePaymentSystem.main(OnlinePaymentSystem.java:19)
BUILD SUCCESSFUL (total time: 0 seconds)

This is the new error under the following changed made. According to the following information 21.3.5.6. Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J I used the default settings. While using the default settings I receive the above error message.

Changes: I did check to make sure that MySQL was running.

 
Charles Sexton
Ranch Hand
Posts: 272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that its a connection problem but don't know on what level. After research I think the problem lies with the Installation of MySQL. When I open Windows command line and type C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql.exe

I get the following error:

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

What do you guys think? I searched but cannot find an answer
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jdbc:mysql://

I'm no MySQL expert, but that doesn't look like a valid URL, either. Even if you want to leave out the host, port and database name (which I wouldn't do), the slash in front of the DB name doesn't look optional (so you'd have 3 slashes).

Access denied for user ''@'localhost' (using password: NO)

That's a typical MySQL issue. This is the first search result I get for that: http://dev.mysql.com/doc/refman/5.6/en/access-denied.html. It mentions what to do about that.

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

That's because of the spaces in the command. Either enclose the entire commend in double quotes, or put the directory into your PATH (I would advise the latter).
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to all for not replying...for reasons best known to itself, thunderbird had dumped this entire thread in Spam folder and I hadn't noticed till now!

@Charles Sexton: As Ulf mentioned, don't leave out the database name, because there is nothing like a 'default database'. The only reason to leave out database from URL is when you want to create a new database.
Also, if the MySQL installation's default configuration has not been changed, then the only valid username is "root" and there will be no password; it can't be left out from getConnection() call. From your first post, it looks like "root" does have a password "h2o". So it's probably just the wrong URL:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic