Connecting to a mysql database

Thread Solved

Join Date: Jan 2009
Posts: 103
Reputation: jakx12 is an unknown quantity at this point 
Solved Threads: 4
jakx12's Avatar
jakx12 jakx12 is offline Offline
Junior Poster

Connecting to a mysql database

 
0
  #1
21 Days Ago
Ok so what i want to be able to do is connect to a mysql database. Here's my code so far:

  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; 21 Days Ago at 2:15 pm.
There are 10 types of people in this world, those that understand binary and those that don't :).
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,341
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 250
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #2
21 Days Ago
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
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 103
Reputation: jakx12 is an unknown quantity at this point 
Solved Threads: 4
jakx12's Avatar
jakx12 jakx12 is offline Offline
Junior Poster
 
0
  #3
21 Days Ago
ok thanks, but were would i find the driver class?
There are 10 types of people in this world, those that understand binary and those that don't :).
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
Join Date: Feb 2006
Posts: 2,341
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 250
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #5
21 Days Ago
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; 21 Days Ago at 6:58 pm.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC