MySQL Connector/J

  • Thread starter Claudio Penasio Junior
  • Start date
C

Claudio Penasio Junior

Hello all

I can't connect on MySQL whith MySQL Connector/J...

code frame...
------------------------------------------------------
String url = "jdbc:mysql://localhost:3306/banco";
String login = "blah";
String pass = "blah";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(url,login,pass);
 
S

Sudsy

Claudio said:
Hello all

I can't connect on MySQL whith MySQL Connector/J...

code frame...
------------------------------------------------------
String url = "jdbc:mysql://localhost:3306/banco";
String login = "blah";
String pass = "blah";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(url,login,pass);

Forget about the newInstance part. Your code should look like this:

try {
Class.forName( "com.mysql.jdbc.Driver" );
}
catch( ClassNotFoundException e ) {
System.err.println( "Could not load driver" );
}
 
N

newsadict

Hi,
It simply means you have to put your code into try-catch block.
try {
Class.forName(("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url,
user,
password);
}
catch (ClassNotFoundException exc) {
System.out.println("blah-blah");
close();
}
catch (SQLException exc) {
System.out.println("SQL-blah-blah");

close();
}
catch (Exception e) {

System.out.println("Something not Class or SQL blah-blah");
close();
}
regards
 
T

Tony Morris

A compile error message is not intended to be a hindrance, more so,
informative. It is useful to read the information that the compiler gives
you.

"exception java.lang.ClassNotFoundException; must be caught or declared
to be thrown Class.forName("com.mysql.jdbc.Driver").newInstance();"

This means that a checked exception called java.lang.ClassNotFoundException
must be declared to be caught or thrown when you call
Class.forName("com.blah").newInstance();

If you don't understand what this means, I strongly suggest that you learn
the fundamentals of exception handling in Java before using JDBC:

http://java.sun.com/docs/books/tutorial/essential/exceptions/

Good luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top