Quote:
Originally posted by blackie
i need to know how is it been used in a sense the String used to connect the MySQL...
|
I just fought with this for awhile.
Give this sample code a try:
import java.sql.*;
import java.util.*;
public class CheckDriverManager {
public static void main(String[] args) {
String driverName = "com.mysql.jdbc.Driver";
Connection conn = null;
//Put in your databasename here
String databaseURL = "jdbc:mysql://localhost/databasename";
//Put in your username and password here
String username = "someuser";
String password = "somepassword";
// Verify that the driver class exists.
try {
Class.forName(driverName);
System.out.println("Did Class.forName()"); //LOGGER
} catch (Exception e) {
e.printStackTrace();
System.out.println("Driver could not be found.");
}
// Get a connection.
try {
conn = DriverManager.getConnection(databaseURL, username, password);
System.out.println("Did getConnection()"); //LOGGER
Statement stat = conn.createStatement();
//Put in your own table name here.
ResultSet result = stat.executeQuery("SELECT * FROM tablename");
result.next();
System.out.println(result.getString(1));
stat.close();
conn.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
System.out.println("Could not get connection."); //LOGGER
}
}
}