Monday, 4 April 2011

MySQL JDBC Connection Properties

There are several things to remember when configuring a MySQL JDBC driver connection. Firstly, you need to specify the driver and the driver class is com.mysql.jdbc.Driver located in the mysql-connector-java-5.x.x.jar archive file and available for download from http://dev.mysql.com/downloads/connector/j/.

You also need to configure an appropriate MySQL url. This has the format:

jdbc:mysql://<hostname>:<port number>/<database-name>

for example:

jdbc:mysql//localhost/testdb

- as the port number is optional.

In order for your Java application to connect to your MySQL you need some code which’ll look abit like this:

      try {
       
Class.forName(driverName);
        con = DriverManager.getConnection
(url, userName, password);
     
} catch (SQLException e) {
etc...

No comments: