iamjerryyeung

Saturday, January 29, 2005

DB2:type4:driver

Listing 1 Use of the legacy/CLI JDBC driver (db2java.zip), a JDBC-type driver to interact with DB2import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Type2ExampleLegacy {
public static void main(String[] args)
{
try
{
// load the DB2 Driver
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
// establish a connection to DB2
Connection db2Conn =
DriverManager.getConnection
("jdbc:db2:phoneDB","db2admin","db2admin");
// use a statement to gather data from the database
Statement st = db2Conn.createStatement();
String myQuery = "SELECT * FROM PHONENUMBERS";
// execute the query
ResultSet resultSet = st.executeQuery(myQuery);
// cycle through the resulSet and display what was grabbed
while (resultSet.next())
{
String name = resultSet.getString("name");
String phoneNumber = resultSet.getString("phonenumber");
System.out.println("Name: " + name);
System.out.println("Phone Number: " + phoneNumber);
System.out.println("-------------------------------");
}
// clean up resources
resultSet.close();
st.close();
db2Conn.close();
}
catch (ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
}
}

0 Comments:

Post a Comment

<< Home