My question is not a profound one, I just want to know how to get this method to work.
I'm running Windows 2k trying to connect to a DB2 server. Right now I'm just trying to get sample code to work, but I guess I must be using the driver improperly or something because it just won't work no matter what I do.
Here's the code:
------------------
import java.sql.*;
import java.awt.*;
import java.applet.Applet;
public class DB2Applt extends
Applet {
static {
try {
// register the driver with DriverManager
// The newInstance() call is needed for the sample to work with
// JDK 1.1.1 on OS/2, where the Class.forName() method does not
// run the static initializer. For other JDKs, the newInstance
// call can be omitted.
Class.forName("COM.ibm.db2.jdbc.net.DB2Driver").newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
Connection con;
public void init() {
try {
// get parameter values from the html page
String server = getParameter("server");
String port = getParameter("port");
// construct the URL ( sample is the database name )
String url = "jdbc:db2://"+server+":"+port+"/sample";
String userid = getParameter("userid");
String password = getParameter("password");
// connect to database with userid and password
con = DriverManager.getConnection(url, userid, password );
} catch( Exception e ) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
try {
// retrieve data from database
g.drawString("First, let's retrieve some data from the database...", 10, 10);
Statement stmt = con.createStatement();
//delme
g.drawString("statement created.", 10, 25);
ResultSet rs = stmt.executeQuery("SELECT * from employee");
g.drawString("Received results:", 10, 25);
// display the result set
// rs.next() returns false when there are no more rows
int y = 50;
int i = 0;
while (rs.next() && (i<2)) {
i++;
String a= rs.getString(1);
String str = rs.getString(2);
String oneLine = " empno= " + a + " firstname= " + str;
g.drawString(oneLine, 20, y );
y = y + 15;
}
stmt.close();
// update the database
g.drawString("Now, update the database...", 10, 100);
stmt = con.createStatement();
int rowsUpdated = stmt.executeUpdate("UPDATE employee set firstnme = 'SHILI' where empno = '000010'");
// display the number of rows updated
String msg = "Updated " + rowsUpdated;
if (1 == rowsUpdated)
msg = msg +" row.";
else
msg = msg +" rows.";
y = y + 40;
g.drawString(msg, 20, y);
stmt.close();
} catch( Exception e ) {
e.printStackTrace();
}
}
}
I've done some
Java programming before, but never worked with databases, so I'm a little lost. There's a daunting amount of information on the net, but no one I can really talk to about this since I'm currently interning at a Japanese company in Tokyo, and my Japanese, isn't really all that great. Thanks for giving it a look.
humbly,
john