ggankhuy
April 30th, 2009, 06:39 PM
I got the following code below:
Can someone help me?
import java.sql.*;
public class Connect
{
public static void main(String[] args)
{
Connection conn = null;
String url = "jdbc:mysql://localhost/cookbook";
String userName = "cbuser";
String password = "cbpass";
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, userName, password);
System.out.println("Connected");
}
catch (Exception e)
{
System.err.println("Cannot connect to server: " + e.getLocalizedMessage());
System.err.println(e.getMessage());
System.err.println(e.toString());
System.err.println(e.getCause());
}
finally
{
if (conn != null)
{
try
{
conn.close();
System.out.println("Disconnected");
}
catch (Exception e)
{
System.out.println("Warning: Failure closing");
}
}
}
}
}
PROGRAM OUTPUT:::
root@tester-laptop:~/workspace/Connect# java Connect
Cannot connect to server: com.mysql.jdbc.Driver
com.mysql.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
null
Can someone help me?
import java.sql.*;
public class Connect
{
public static void main(String[] args)
{
Connection conn = null;
String url = "jdbc:mysql://localhost/cookbook";
String userName = "cbuser";
String password = "cbpass";
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, userName, password);
System.out.println("Connected");
}
catch (Exception e)
{
System.err.println("Cannot connect to server: " + e.getLocalizedMessage());
System.err.println(e.getMessage());
System.err.println(e.toString());
System.err.println(e.getCause());
}
finally
{
if (conn != null)
{
try
{
conn.close();
System.out.println("Disconnected");
}
catch (Exception e)
{
System.out.println("Warning: Failure closing");
}
}
}
}
}
PROGRAM OUTPUT:::
root@tester-laptop:~/workspace/Connect# java Connect
Cannot connect to server: com.mysql.jdbc.Driver
com.mysql.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
null