Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using db2jcc.jar

Status
Not open for further replies.

Faroon

Programmer
Jun 16, 2003
46
CA
Hi, I have the file db2jcc.jar sitting in my home directory, and have installed java 1.3.1 on my system. How can I use this db2jcc file to get data from a remote db2 server? Can you please post sample code (I was able to do it with a java applet, but I want my program to output the result to a text file). The code I wrote:

import java.sql.*;
import java.awt.*;

public class test {
public static void main(String args[]) {
int i = 0;
Connection con = null;
System.out.println("Connecting...");
try {
String url = "jdbc:db2://host:port/db_name";
Class.forName("COM.ibm.db2.jcc.DB2Driver").newInstance();
con = DriverManager.getConnection(url, "usr", "pswd");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM tbl");
System.out.println("Retrieving data");

while (rs.next() && (i < 15)) {
System.out.println(i+&quot;:&quot;+rs.getString(1));
i ++;
}
stmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}

$echo $CLASSPATH outputs /home/mydir/db2jcc.jar
$javac test.java -> runs fine
$java test -> java.lang.NoClassDefFoundError: test

What did I do wrong?
 
It's ok, I figured it out now. If you set CLASSPATH on your system, then Java won't run (thus produce that error). But without CLASSPATH, then the com.ibm.db2.jcc.DB2Driver class can't be found (which will produce an error saying the driver can't be found). What I did was remove the CLASSPATH and then extract all the files in the db2jcc.jar archive to a folder in the current directory. Compile and run the code above again, worked great. Hope this helps somebody with same problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top