944,004 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1048
  • Java RSS
Nov 3rd, 2009
0

Connecting to a mysql database

Expand Post »
Ok so what i want to be able to do is connect to a mysql database. Here's my code so far:

Java Syntax (Toggle Plain Text)
  1. Connection connection = null;
  2. try {
  3. // Load the JDBC driver
  4. String driverName = "postgresql-8.4-701.jdbc4.jar"; // MySQL MM JDBC driver
  5. Class.forName(driverName);
  6.  
  7. // Create a connection to the database
  8. String serverName = "mysql10.000webhost.com";
  9. String mydatabase = "a2362883_test";
  10. String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url
  11. String username = "a2362883_admin";
  12. String password = "test";
  13. connection = (Connection) DriverManager.getConnection(url, username, password);
  14. System.out.println("Connected");
  15. }
  16. catch (ClassNotFoundException e) {
  17. System.out.println("Could not find the database driver");
  18. } catch (SQLException e) {
  19. System.out.println("Could not connect to the database");
  20. }

The problem is though that it keeps saying it could not find the database driver, even though i put postgresql-8.4-701.jdbc4.jar in the project file. So therefore it should find it, or is this not the driver? Im using java 1.6, snow leopard os and eclipse. By the way, the password isnt test but im not going to tell you it

Thanks,

jakx12.
Last edited by jakx12; Nov 3rd, 2009 at 2:15 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 5
Junior Poster
jakx12 is offline Offline
123 posts
since Jan 2009
Nov 3rd, 2009
0
Re: Connecting to a mysql database
You don't load the jar, you load the Driver class and include the jar on the classpath. See the Tutorial and the MySQL manual for MySQL specific examples
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 3rd, 2009
0
Re: Connecting to a mysql database
ok thanks, but were would i find the driver class?
Reputation Points: 10
Solved Threads: 5
Junior Poster
jakx12 is offline Offline
123 posts
since Jan 2009
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Nov 3rd, 2009
0
Re: Connecting to a mysql database
The MySQL documentation shows you which class, at the very beginning of the page I linked to, and the tutorials (and the MySQL docs) show you how to use it. Read those.

Edit: Besides, why would you be loading a postgresql Driver (jar) when it is mysql (according to your title) that you want to connect to?
Last edited by masijade; Nov 3rd, 2009 at 6:58 pm.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: HELP with code
Next Thread in Java Forum Timeline: Java ActionListener Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC