• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

JDBC MySQL Driver Connection

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I designed a user interface using java and at this point I am trying to connect it to a mySQL database on my computer just for development purposes. After that is complete I will be putting the database on a server and running it from there.

In the meantime I can't seem to find the right url to put into the Class.forName() call so my database is not actually connecting (keeps throwing the ClassNotFoundException). I commented out a few of the other urls I tried to use....all your help would be greatly appreciated...the connection class code is below:

(For reference I installed the mysql-connector-java-3.1.10 version)

import java.sql.*;


public class Connect {
public static void main(String argv[]) {
Connection con = null;


Connection connection = null;
try {
//Load the JDBC driver
//String driverName = "mysql-connector-java-3.1.10//src//com//mysql//jdbc//Driver"; //MySQL MM JDBC driver
//mm.mysql.jdbc.Driver

String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName).newInstance();

//Create a connection to the database
String serverName = "localhost";
String mydatabase = "prop";
String url = "jdbc:mysql:" + serverName + "/" + mydatabase; //a JDBC url
String username = "username";
String password = "password";
connection = DriverManager.getConnection(url, username, password);

} catch (SQLException e) {
//Could not connect to the database
System.out.println("Connection Not Found");
}
catch (ClassNotFoundException e) {
System.out.println("Driver Not Found");
//Could not find the database driver}}
}
catch (Exception ex){
System.out.println("ex");
}
System.out.println("Connection:" + connection);
}
}
 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

n the meantime I can't seem to find the right url to put into the Class.forName()



You don't put a URL into the Class.forName(); you give it the class name of the driver -- which I see you have done in your code.

if it's giving you a ClassNotFoundException, that means that the dirver class is not part of the classpath.

Have you placed the mysql jar file in the classpath?
[ July 19, 2005: Message edited by: Bear Bibeault ]
 
Michael Poke
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Have you placed the mysql jar file in the classpath?"

how would I go about doing that?
 
author & internet detective
Posts: 42160
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Poke:
"Have you placed the mysql jar file in the classpath?"

how would I go about doing that?


Michael,
Welcome to JavaRanch!

You could use java -cp "myClasspath" MyClass to run at the command line. If you expect to use mysql a lot (as is likely), you could add it to the operating system environment variable.
 
Michael Poke
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I did was right click on my computer, properties, advanced tab, environment variables and then added the jar file there.

I am still not getting a connection...I do not understand what is going wrong.

Help Please!?!?!?!
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you could try the next line :
javac -classpath . LoadDriver.java (example, replace the name with yours)
and then.
java -classpath "mysql-connector-java-3.1.10-bin.jar;" LoadDriver

i hope this could help u.
 
Michael Poke
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
still not working

anymore ideas?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A word of advice: Just saying "it doesn't work" will get you nowhere. You need to specify precisely what you're doing, what the error messages are, what your classpath is etc. Only then can one make educated guesses what the problem might be.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic