[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.dtp] Re: Connect to MySql 5.0

Jalal ha scritto:
Hi,
Is it possible to connect to MySql 5.0, I am using eclipse 3.2.1. If yes, What plug-ins Do I need to install ,and is there any tutorial on how do to make the conneciton.
Thanks.



Hi,
I am using MySQL 5.0 with eclipse 3.2.0 and the mysql-connector-java-3.1.13 is in the classpath; you can find it at http://dev.mysql.com/downloads/ (now the http://dev.mysql.com/downloads/connector/j/5.0.html is available).


To establish the connection I have used at first the DriverManager class, and now the DataSource object.

DriverManager:

public static Connection creaConnessione()
{
try {
if (resourceBundle == null) {
try {
resourceBundle = ResourceBundle.getBundle("db");
} catch (Exception e) {
visualizzaErrore();
}
}
dbProvider = getDbProvider();
// postgreSQL
if (dbProvider.equals(postgreSQL)) {
..... posgreSQL Statements....
} else if (dbProvider.equals(MySQL)) {
DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
String connessione = resourceBundle.getString("mconnessione");
String database = resourceBundle.getString("mdatabase");
String user = resourceBundle.getString("muser");
String pwd = resourceBundle.getString("mpwd");
con = DriverManager.getConnection(connessione + database, user, pwd);
// DB2
} else if (dbProvider.equals(DB2)) {
... DB2 Statements ...
// Sconosciuto
} else {
System.err.println("Database sconosciuto - controllare db.properties ");
}
return con;
} catch(java.sql.SQLException e) {
System.err.print("Connessione database non riuscita: ");
System.err.println(e.getMessage());
}
return null;
}
public static String getDbProvider() {
if (resourceBundle == null) {
try {
resourceBundle = ResourceBundle.getBundle("db");
} catch (Exception e) {
visualizzaErrore();
}
}
return resourceBundle.getString("dbprovider");
}


Now I use a DataSource object to establish a PooledConnection; I have registered the MysqlConnectionPoolDataSource in the File System, and this is the code to establish the connection:

	/**
	 * Crea la connessione con il database tramite una Data Source
	 *
	 */
	public static Connection creaPoolConn()
    	{
    		if (pcon == null) {
    			pcon = creaPCon();
    		}
    		try {
			con = pcon.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
        	return con;
    	}
	/**
	 * Crea la PooledConnection
	 *
	 */

public static PooledConnection creaPCon()
{
dbProvider = getDbProvider();
try {
Context context = createContext();
// MySQL
if (dbProvider.equals(MySQL)) {
MysqlConnectionPoolDataSource dsource = (MysqlConnectionPoolDataSource) context.lookup("mydatabase");
pcon = dsource.getPooledConnection();
// DB2
} else if (dbProvider.equals(DB2)) {
... DB2 Statements ...
} else {
System.err.println("Database sconosciuto - controllare db.properties ");
}
} catch (Exception e) {
e.printStackTrace();
}
return pcon;
}

private static Context createContext() throws NamingException {
Context context = null;
try {
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
env.setProperty(Context.PROVIDER_URL, "file:E:/JNDI/JDBC");
context = new InitialContext(env);
} catch (Exception e) {
e.printStackTrace();
}
return context;
}


The resourceBundle refers to the db.properties file:

# Configurazione DataBase e connessione
# Database/connessione/Driver per MySQL
mconnessione = jdbc:mysql://localhost:3306/
mdatabase = mydatabase
muser = myuser
mpwd = mypwd
dbprovider = MySQL

To register the DataSource and access it from File System, you must obtain JNDI classes that support the file system, for example Sun's fscontext.jar (which requires providerutil.jar as well) and add these to your classpath (See the Java Naming and Directory Interface(TM) tutorial at http://java.sun.com/docs/books/tutorial/jndi/index.html).

A tutorial on JDBC is here: http://java.sun.com/docs/books/tutorial/jdbc/TOC.html


Hope this helps Riccardo Cattania www.riccardocattania.net