Java Tutorial - Java Scipt : Testing the Connection Parameters

Java Tutorial - Java Scipt :

Testing the Connection Parameters


Before we get started configuring the Data Sources, we need to know the following things:

·         The class name of the JDBC driver

·         The connection URL

·         The username we will use to connect to the database

·         The password for the user

We also need to pick a name that we will use to look up the connection in JNDI. For accessing a MySQL database named mysqltest running on the same computer as Tomcat, we will use the values shown in Table 8.4.
localhost can be replaced by the name of the server running the database if it is not the same machine that Tomcat is running on.

If we were to access the database on an embedded HSqlDb, the values would be as shown in Table 8.5.

Before we configure these into Tomcat, it makes sense to test the parameters to make certain they are correct. The following code is a useful utility that can be run from the command line to test the parameters:

import java.sql.*;
public class JdbcTest
{
public static void main(String[] args)
{
String driver = “org.hsqldb.jdbcDriver”;
String url = “jdbc:hsqldb:d:/openjava/hsqldb/data/hsqltest”;
String user = “sa”;
String passwd = “”;
if( args.length > 0 && args[0] != “” )
driver = args[0];
if( args.length > 1 && args[1] != “” )
url = args[1];
if( args.length > 2 && args[2] != “” )
user = args[2];
if( args.length > 3 && args[3] != “” )
passwd = args[3];
System.out.println( “Driver: “+driver );
System.out.println( “URL : “+url );
System.out.println( “User : “+user );
System.out.println( “Passwd: “+passwd );
Connection conn = null;
DatabaseMetaData meta = null;
try
{
System.out.println(“Loading Driver: “+driver);
Class.forName( driver ).newInstance();
conn = DriverManager.getConnection(url, user, passwd);
System.out.println( “Got connection” );
meta = conn.getMetaData();
System.out.println( “ProductName ==>”+
meta.getDatabaseProductName());
System.out.println( “DriverName ==>”+meta.getDriverName());
System.out.println( “DriverVersion==>”+meta.getDriverVersion());
System.out.println( “URL ==>”+meta.getURL());
System.out.println(“*** TABLES ***”);
ResultSet rs = meta.getTables(null, null, “%”, null);
while( rs.next() )
{
System.out.print( rs.getString(“TABLE_TYPE”));
System.out.print( “:” );
System.out.println( rs.getString(“TABLE_NAME”));
}
System.out.println( “Closing connection”);
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
The program accepts four command-line parameters:

·        The name of the JDBC Driver class

·        The URL for the connection

·        The user ID

·        The password

If no arguments are passed in, it will try to connect to an HSqlDb database. The following line loads the database driver into the JVM:

Class.forName( driver ).newInstance();

The driver must be on the classpath. The following line gets the connection, based on the provided URL:

conn = DriverManager.getConnection(url, user, passwd);

The rest of the program uses the DatabaseMetaData object from the connection to print information about the JDBC driver and the database. To run the program to test the MySQL sample database that we set up earlier, enter the following command at the command-line prompt:

java -cp mysql-connector-java-2.0.14-bin.jar JdbcTest \
com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/mysqltest \
tomcat passwd5

Note that this should be entered all as one line. The result should be something similar to the following:

Driver: com.mysql.jdbc.Driver
URL : jdbc:mysql://localhost:3306/mysqltest
User : tomcat
Passwd: passwd5
Loading Driver: com.mysql.jdbc.Driver
Got connection
ProductName ==>MySQL
DriverName ==>Mark Matthews’ MySQL Driver
DriverVersion==>2.0.14
URL ==>jdbc:mysql://localhost:3306/mysqltest
*** TABLES ***
TABLE:quotes
Closing connection

If this works, then we are ready to configure a DataSource in Tomcat.