Hej
Jeg lavede engang en Factory til at få fat i MySQL.
Jeg brugte denne url streng:
jdbc:
mysql://localhost/test?user=root&password=da min user 'root' ingen password har.
Det er du samme version som du har jeg kører på.
/**
*
*
* @author Claus Ibsen
* @version $Id$
*/
package com.cib.sql;
import java.sql.*;
// todo: a generic connection factory so it's possible to make a factory for DB2, Oracle and so on.
/**
* This factory is used to get a java.sql.Connection to the MySQL database.
* <p/>
* You can set the MySQL database connection url string using the system property with
* the key named as {@link MySQLConnectionFactory#SYS_PROP_CONNECTION_URL}.
* <p/>
* If the system property is not defined the default connection url as: <code>jdbc:
mysql://localhost/test?user=root&password=</code> is used.
*/
public class MySQLConnectionFactory {
public static final String SYS_PROP_CONNECTION_URL = "com.cib.sql.ConnectionURL";
private static String CONNECTION_URL = "jdbc:
mysql://localhost/test?user=root&password="; // Load the JDBC driver and init the connection url
static {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
if (System.getProperty(SYS_PROP_CONNECTION_URL) != null) {
CONNECTION_URL = System.getProperty(SYS_PROP_CONNECTION_URL);
}
} catch (Exception e) {
System.err.print("Error getting MySQL JDBC Driver: trace:\n");
e.printStackTrace();
}
}
/**
* Private constructor to use static factory insteed.
*/
private MySQLConnectionFactory() {
}
/**
* Get's a new connection to the MySQL database
* @return a connection to the database
* @throws SQLException error getting the connection
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(CONNECTION_URL);
}
}