• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need Help Establishing a Connection with SQL

 
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a MySQL server running on your own machine?
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic