This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes The getConnection method is trying to ruin my life! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "The getConnection method is trying to ruin my life!" Watch "The getConnection method is trying to ruin my life!" New topic
Author

The getConnection method is trying to ruin my life!

John Pacheco
Greenhorn

Joined: Jul 13, 2001
Posts: 7
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
Julio Lopez
Greenhorn

Joined: Nov 14, 2000
Posts: 28
I do a lot of DB/2 JDBC work on a Win 2K box. Tell me what sort of errors are you getting? Your code looks OK. Also, have you tried using the COM.ibm.db2.jdbc.app.DB2Driver instead? Let me know and I will see if I can help you out.
Julio Lopez
M-Group Systems
John Pacheco
Greenhorn

Joined: Jul 13, 2001
Posts: 7
Oh sorry, here's the error that I'm getting. I've got get.Connection is throwing an exception. Here it is (quoted phrase translated from Japanese. I'm sure the standard english one is pretty close.) :
-----------------------------------------------------
COM.ibm.db2.jdbc.net.DB2Exception: [IBM][JDBC Driver] CLI0616E "Error opening socket." SQLSTATE=08S01
at java.sql.DriverManager.getConnection(DriverManager.java:473)
at java.sql.DriverManager.getConnection(DriverManager.java:137)
at DB2Applt.init(DB2Applt.java:74)
at sun.applet.AppletPanel.run(AppletPanel.java:342)
at java.lang.Thread.run(Thread.java:479)
-----------------------------------------------------
Any ideas?
Thank you very much,
John
John Pacheco
Greenhorn

Joined: Jul 13, 2001
Posts: 7
My mistake. That was an error I got while trying to mess around with something else. Here's the real error(again quoted phrase is being translated roughly from the japanese on this machine):
-----------------------------------------------------
COM.ibm.db2.jdbc.net.DB2Exception: [IBM][JDBC Driver] CLI0615E "Error recieving from socket. Sever is not responding." SQLSTATE=08S01
at java.sql.DriverManager.getConnection(DriverManager.java:473)
at java.sql.DriverManager.getConnection(DriverManager.java:137)
at DB2Applt.init(DB2Applt.java:74)
at sun.applet.AppletPanel.run(AppletPanel.java:342)
at java.lang.Thread.run(Thread.java:479)
-----------------------------------------------------
Sorry bout the mix up.
yoroshiku onegaishimasu,
John
Nizan Horsefield
Ranch Hand

Joined: Jun 05, 2001
Posts: 42
John,
Make sure the "DB2 JDBC applet server" service is started. Best to make this start automatically (go to services).
Then on a command prompt, type
db2jstrt 6789

Nizan Horsefield<BR>Sys Eng Internet Services<BR>Citibank
John Pacheco
Greenhorn

Joined: Jul 13, 2001
Posts: 7
Yeah it's running and set up to start automatically.
Thanks though. Any other ideas?
Yoroshiku onegaishimasu,
John
Julio Lopez
Greenhorn

Joined: Nov 14, 2000
Posts: 28
I don't have much experience with the net driver. If I was you I would try out the app driver. To use the app driver you will need to install DB/2 CAE or DB/2 run time client on your Win 2K machine. After installation go to X:/SQLLIB/java12 (where X is the drive where the DB/2 run time client is installed) and run "usejdbc2". Then go to My Computer right click and choose Properties. Click the Advanced tab and choose Environment Variables. Make sure that X:/SQLLIB/java/db2java.zip is in the classpath. Do a reboot and the services should start automatically. Open the Control Center, right click on Systems and choose add. Under host name enter in the IP address of the DB/2 server and click retrieve. The server information should be retrieved and filled into the text boxes, then click OK. In the Instance folder right click and choose add. Click refresh and then click OK. Highlight the Databases tag, right click and choose add. Click refresh and choose the database you want to query. Now go back to your java code and instead of "COM.ibm.db2.jdbc.app.DB2Driver" in the Class.forName statement use "COM.ibm.db2.jdbc.app.DB2Driver" And the URL should be jdbc:db2:XDATABASE (where XDATABASE is the nickname of the database that you selected in the client (if not specified the nickname is the same as the name) ) Give that a try and let me know. If this gives you the same error there must be some sort of service not running properly on the server.
Julio Lopez
M-Group Systems
pellmika
Greenhorn

Joined: Jul 16, 2001
Posts: 2
The URL format should be
jdbc:your_type_of_driver:your_db_alias
instead of :
String url = "jdbc:db2://"+server+":"+port+"/sample";
Just a clue : Can applet open sockets ( other than the server it came from ) to make the getConnection() call? If not, you cannot make 3-tiers call from your applet.
Hope will help!
John Pacheco
Greenhorn

Joined: Jul 13, 2001
Posts: 7
the app driver seems to work. That's all well and good, but this program (if I can ever get the connection up and running) is intended to be an applet that we want to put on the company server to allow fast and easy access to the database. Correct me if I'm wrong, but I don't think that's possible with the app driver right?
thanks,
-john
John Pacheco
Greenhorn

Joined: Jul 13, 2001
Posts: 7
Thanks for your reminding me about the limitations of sockets pellmika, I had forgotten that and probably would have wondered why I couldn't do certain things that I might try later on, but as of yet it isn't too much of an issue.
As for the url. Well I tried it the way you had it writen, but according to Sun's javadocs on the DriverManager Class I'm pretty sure that you're supposed to write it:
jdbc:"subprotocol""subname"
And since the format that I have there is the one that came written on the sample applet, then I'm slightly inclined to trust it.

[This message has been edited by John Pacheco (edited July 16, 2001).]
John Pacheco
Greenhorn

Joined: Jul 13, 2001
Posts: 7
got it all fixed guys, thanks for your help! I owe you big time.
John
Anders Domeij
Greenhorn

Joined: Sep 29, 2001
Posts: 4
John,
I was having the same problem as you posted originally. Thanks to this thread I solved my problem too.
I managed to get it running on my machine which has a DB2 Server installed on it.
The trick is to do the db2jstrt xxxx ( where xxxx is the portnumber on which the DB2 server will listen for requests to the DB2 java applet server).
Then you can can use the 'jdbc:db2:servernam ort/database' notation, bearing in mind that port must be whatever you chose as xxxx in the db2jstrt command.
In my case I issued the cpmmand 'db2jstrt 6789' as suggsted here and then used the following in my Java program
Class.forName("COM.ibm.db2.jdbc.net.DB2Driver").newInstance();
String url = "jdbc:db2://localhost:6789/sample";
java.sql.Connection con = DriverManager.getConnection(url, userid, password);
where userid and password are set to a valid DB2 user.
At first this threw an SQL Exception -- "NO START DATABASE COMMAND ENTERED" -- which at least made sense -- db2start solved that in a hurry.
Now this leads to a question, what functionallity do I lose (if any) compared to using the CAE version of JDBC access i.e
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
String url = "jdbc:db2://localhost:50000/sample"
??

------------------
Anders Domeij
Greenhorn

Joined: Sep 29, 2001
Posts: 4
Whoops sorry about the smiley in the middle of the previous post
it actually is the text 'jdbc:db2:servernam:port/database'
------------------
Cedric Bonnin
Greenhorn

Joined: Jun 05, 2002
Posts: 1
Originally posted by Nizan Horsefield:
John,
Make sure the "DB2 JDBC applet server" service is started. Best to make this start automatically (go to services).
Then on a command prompt, type
db2jstrt 6789

Hi, how can I be sure that the "DB2 JDBC applet server" is started?
Thanks
C�dric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The getConnection method is trying to ruin my life!