Thursday, April 9, 2009

Java MySQL connection

This tutorial helps you connect your java applications with MySQL. So here goes, you need a mysql connector/j found in this page.

Go to Connector Page

And put the jar file inside this directory "jre/lib/ext". Should be around your java installation folder.

And with a help of this simple java snippet,


Connection conn = null;
try
{
String url = "jdbc:mysql://localhost/databasename?user=username&password=yourpassword";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url);
}
catch (SQLException se)
{
System.out.println(se);
}
catch (Exception ex)
{
System.out.println(ex);
}


Your java applications can now connect to MySQL servers.

No comments:

Post a Comment