If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > problem with java-mysql connection

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-27-05, 02:53
albert85 albert85 is offline
Registered User
 
Join Date: Dec 2005
Posts: 6
problem with java-mysql connection

i have the problem of the connection java to MySQL
i oledi download the entire software:
-J2SE development kit 5.0 update6.0
-MySQL 4.1.16
-MySQL connector/j 3.1.12
is it enough for me to connect the java application to MySQL??
i oledi set the classpath like that:
my computer right click>properties >advanced >Environment Variables> System Variables>
create CLASSPATH and key in my .jar url which is:
C:\mysql-connector-java-3.1.12\mysql-connector-java-3.1.12-bin.jar
is it i inthe right path?
when i compile the source code, the error line come out:
Test.java:20: unreported exception java.lang.ClassNotFoundException; must be cau
ght or declared to be thrown
Class.forName ("JDBC_DRIVER");
^
1 error

And my source code is:::
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class Test
{
static final String JDBC_DRIVER = "org.gjt.mm.mysql.driver";
static final String DATABASE_URL = "jdbc:mysql://localhost/test";

public static void main( String args[] )
{
Connection connection = null;
Statement statement = null;

try
{
Class.forName ("JDBC_DRIVER");
connection =
DriverManager.getConnection(DATABASE_URL,"root"," ");
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(
"SELECT name,last_name FROM t");
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
System.out.println( " Name is:");
for ( int i=1; i <= numberOfColumns;i++)
System.out.printf( "%-8s\t",metaData.getColumnName(i));
System.out.println();
while (resultSet.next())
{
for(int i=1;i <= numberOfColumns; i++)
System.out.printf("%-8s\t",resultSet.getObject(i));
System.out.println();
}
}
catch ( SQLException sqlException )
{
sqlException.printStackTrace();
System.exit(1);
}
finally
{
try
{
statement.close();
connection.close();
}
catch (Exception exception)
{
exception.printStackTrace();
System.exit(1);
}
}
}
}


What the problem of me? can someone who can solve this problem guide me to the right path
thank you..
Reply With Quote
  #2 (permalink)  
Old 01-03-06, 02:26
urmaniac urmaniac is offline
Registered User
 
Join Date: Dec 2004
Location: Malaysia
Posts: 24
are u sure this code is correct??
~~Class.forName ("JDBC_DRIVER");~~~
Reply With Quote
  #3 (permalink)  
Old 01-03-06, 02:28
urmaniac urmaniac is offline
Registered User
 
Join Date: Dec 2004
Location: Malaysia
Posts: 24
Reply With Quote
  #4 (permalink)  
Old 01-03-06, 10:08
jfulton jfulton is offline
Registered User
 
Join Date: Apr 2005
Location: Baltimore, MD
Posts: 297
The line of code which urmaniac pointed out is your problem.

Check out the javadocs for Class.forName(). It returns a reference to the class passed in. Your usage of it does not assign it to anything and doesn't really do much. Also, you failed to catch any of the exceptions it throws. You need to add more catch statements before your finally clause or get rid of that line of code.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On