The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
July 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Having trouble connecting to a database

Posted by John Pacheco on July 13, 2001 at 4:31 AM

My question is not a profound one, I just want to know how to get the getConnection method to work.

I'm running Windows 2k trying to connect to a DB2 server. Right now I'm just trying to get the given sample code to work, but I guess I must be using the driver improperly or something because it just won't connect 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 {
// I've tried using the IP address and I've tried the hostname.
String server = "dosv_169";
String port = "50000";

// construct the URL ( issdb is the database name )
String url = "jdbc:db2://"+server+":"+port+"/issdb";

String userid = "db2admin";
String password = "db2admin";

// connect to database with userid and password
// This never works... What might I be doing wrong??
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();
// "iss_insurance_company_tbl" is our test data.
ResultSet rs = stmt.executeQuery("SELECT * from iss_insurance_company_tbl");
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 ) {
g.drawString("Error:" + e.toString(), 10, 25);
}
}
}


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





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us