Registering and Connecting the Driver
Copy and paste the correct version of this code into your own when registering the driver with the drivermanager and obtaining a connection from the drivermanager.
import java.sql.*;
// Notice, do not import org.gjt.mm.mysql.*
// or you will have problems!
public class LoadDriver
{
public static void main(String[] Args)
{ try { // The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.Driver").newInstance(); [use this code for MySQL]
Class.forName("org.postgresql.Driver").newInstance(); [use this code for PostgreSQL]
}
catch (Exception E) {
System.err.println("Unable to load driver.");
E.printStackTrace();
try {
Connection C = DriverManager.getConnection
("jdbc:mysql://mysql.yourdomainname/database_com_au?
user=user_name&password=password"); [use this code for MySQL]
Connection C = CreateConnection
("jdbc:postgresql://pgsql.yourdomainname/database_com_au?
user=user_name&password=password"); [use this code for PostgreSQL]
// Do something with the Connection
....
}
catch (SQLException E) {
System.out.println("SQLException: " + E.getMessage());
System.out.println("SQLState: " + E.getSQLState());
System.out.println("VendorError: " + E.getErrorCode());
}
It is important to register the driver with the DriverManager, and to acquire a URL for the database using the following syntax:
jdbc:mysql://mysql.yourdomainname/database_com_au?user=
user_name&password=password
[use this code for MySQL]
jdbc:postgresql://pgsql.yourdomainname/database_com_au?user=
user_name&password=password
[use this code for PostgreSQL]
When the URL is set up and identified, it is forwarded to the DriverManager.getConnection() method so that a Connection Object can be attained.
** Note: Your username and password are available from Web Secure Support.
|