Approaches to Web Development for Bioinformatics
Java supports access to relational databases via several interfaces, the most commonly used of which is
the Java Database Connectivity (JDBC) API. This API is an integral part of the base Java
platform. The JDBC API is documented on the Sun website16.
Drivers for different databases each implement this API and bundle the driver as a jar file.
The one that I will demonstrate here is for MySQL. You can read about and download the
MySQL JDBC driver, MySQL Connector/J, at the MySQL web site40.
Make sure that you have it on your class path when you run the code.
Here is an example Java program that retrieves the complete list of gene_info records in the table defined
about and prints them out to the console. The Java code is in file
GeneInfoDAO.java. The compiled code is in file
GeneInfoDAO.class.
The program should be run from the command line in stand-alone mode. Don't forget to replace the name
and password of the user in your version.
import java.sql.*;
public class GeneInfoDAO {
public void printGeneInfo() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost/genes?" +
"user={USER_ABC}&password={ABC}");
stmt = conn.createStatement();
rs = stmt.executeQuery(
"SELECT * FROM gene_info");
while (rs.next()) {
int id = rs.getInt("id");
String symbol = rs.getString("symbol");
String name = rs.getString("name");
System.out.println("Gene, id: " + id +
", symbol: " + symbol +
", name: " + name);
}
}
catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
ex.printStackTrace();
}
catch (ClassNotFoundException ex) {
System.err.println("ClassNotFoundException: " + ex.getMessage());
ex.printStackTrace();
}
finally {
if (rs != null) {
try {
rs.close();
}
catch(Throwable t) {}
}
if (stmt != null) {
try {
stmt.close();
}
catch(Throwable t) {}
}
if (conn != null) {
try {
conn.close();
}
catch(Throwable t) {}
}
}
}
public static void main(String[] argv) {
GeneInfoDAO geneInfoDAO = new GeneInfoDAO();
geneInfoDAO.printGeneInfo();
}
}
A few things are apparent from this example:
-
The example code can be easily modified to be incorporated into a dynamic web page. I have not done this
here to keep the example short. However, that brings out the point that the cost of reusability here is
complexity.
-
Java is well structured, especially with regards to error handling and closing the result set,
statement, and connection within a finally block.
-
The JDBC API's isolates the developer from vendor specific API's. However, we had to register the driver
explicitly with the
Class.forName() call. Within an application server this step is not
necessary. For stand-alone applications there are ways around it too.
-
A new connection was created with the
DriverManager.getConnection(), which also specified
the host name and other connection details. Application servers provide a framework to specify these
parameters within configuration files and make use of a connection pool so that a new connection does
not need to be created every time a user hits a web page that needs a database call.
-
Java is comparatively verbose. There are tools to reduce this.
Hibernate45 and several similar tools
are relational persistence toolset for Java and .NET designed to automate the object
to relational mapping process. However, Hibernate is complex in its own right.
There are no user comments.
Please send ideas and opinions by email at alexamies@gmail.com.
© 2006-2007 Alex Amies