473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Java Type 4 Coonection in V8.x

I'm trying to use the JDBC Universal driver to get a Type 4 connection in a
Java client program running under DB2 V8.2.1 but I keep getting a
"connection refused: connect" message.

I'm running DB2 Personal Edition so I am trying to connect to the database
SQLPROCS on this same machine. I've checked the manual and am imitating the
examples as well as I can but the examples are geared to remote machines,
not the local one, so I strongly suspect that the syntax is somehow
different for local machines. Anyway, here is the relevant code:
/**
* Load the JDBC driver.
*/
public void loadDriver() {

String METHOD_NAME = "loadDriver()";

String JDBC_DRIVER_NAME = "com.ibm.db2.jcc.DB2Driver"; //new Universal
JDBC driver

/* Load the JDBC driver. */
try {
Class.forName(JDBC_DRIVER_NAME).newInstance();
}
catch (Exception excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " - Encountered
error " +
"while attempting to load JDBC driver, " + JDBC_DRIVER_NAME + ".");
excp.printStackTrace();
System.exit(16);
}
}

/**
* Get a connection to the database and set Autocommit.
*/
public void connectToDatabase() {

String METHOD_NAME = "connectToDatabase()";

String DB2_URL = "jdbc:db2:sqlprocs"; //old and new Type 2 URL - works
// String DB2_URL =
"jdbc:db2://localhost/sqlprocs:user=rhino;password=rhino;"; //new Type 4
URL - doesn't work
// String DB2_URL =
"jdbc:db2://127.0.0.1/sqlprocs:user=rhino;password=rhino;"; //new Type 4
URL - doesn't work

boolean AUTOCOMMIT = false;

/* Connect to the database. */
try {
conn01 = DriverManager.getConnection(DB2_URL);
}
catch (SQLException sql_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " - Encountered
error while " +
"connecting to URL, " + DB2_URL + ".");
sql_excp.printStackTrace();
System.exit(16);
}

/* Set autocommit. */
try {
conn01.setAutoCommit(AUTOCOMMIT);
}
catch (SQLException sql_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " - Encountered
error while " +
"setting autocommit to " + AUTOCOMMIT + ".");
sql_excp.printStackTrace();
System.exit(16);
}
}
Only the first version of the DB2_URL variable works; the other two don't.
That means that I can get a Type 2 connection but not a Type 4 connection.
What am I doing wrong?

By the way, I can successfully ping both localhost and 127.0.0.1 from my
Windows command prompt so I don't seem to be missing and etc/ entries.
--
Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare
Nov 12 '05 #1
8 2548
Rhino wrote:
I'm trying to use the JDBC Universal driver to get a Type 4 connection in a
Java client program running under DB2 V8.2.1 but I keep getting a
"connection refused: connect" message.

I'm running DB2 Personal Edition so I am trying to connect to the database
SQLPROCS on this same machine. I've checked the manual and am imitating the
examples as well as I can but the examples are geared to remote machines,
not the local one, so I strongly suspect that the syntax is somehow
different for local machines. Anyway, here is the relevant code:
/**
* Load the JDBC driver.
*/
public void loadDriver() {

String METHOD_NAME = "loadDriver()";

String JDBC_DRIVER_NAME = "com.ibm.db2.jcc.DB2Driver"; //new Universal
JDBC driver

/* Load the JDBC driver. */
try {
Class.forName(JDBC_DRIVER_NAME).newInstance();
}
catch (Exception excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " - Encountered
error " +
"while attempting to load JDBC driver, " + JDBC_DRIVER_NAME + ".");
excp.printStackTrace();
System.exit(16);
}
}

/**
* Get a connection to the database and set Autocommit.
*/
public void connectToDatabase() {

String METHOD_NAME = "connectToDatabase()";

String DB2_URL = "jdbc:db2:sqlprocs"; //old and new Type 2 URL - works
// String DB2_URL =
"jdbc:db2://localhost/sqlprocs:user=rhino;password=rhino;"; //new Type 4
URL - doesn't work
// String DB2_URL =
"jdbc:db2://127.0.0.1/sqlprocs:user=rhino;password=rhino;"; //new Type 4
URL - doesn't work

boolean AUTOCOMMIT = false;

/* Connect to the database. */
try {
conn01 = DriverManager.getConnection(DB2_URL);
}
catch (SQLException sql_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " - Encountered
error while " +
"connecting to URL, " + DB2_URL + ".");
sql_excp.printStackTrace();
System.exit(16);
}

/* Set autocommit. */
try {
conn01.setAutoCommit(AUTOCOMMIT);
}
catch (SQLException sql_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " - Encountered
error while " +
"setting autocommit to " + AUTOCOMMIT + ".");
sql_excp.printStackTrace();
System.exit(16);
}
}
Only the first version of the DB2_URL variable works; the other two don't.
That means that I can get a Type 2 connection but not a Type 4 connection.
What am I doing wrong?

By the way, I can successfully ping both localhost and 127.0.0.1 from my
Windows command prompt so I don't seem to be missing and etc/ entries.


Try this (watch for wrapped lines):

import java.lang.*;
import java.sql.*;

public class MyJccTest
{
public static void main (String args[]) throws Exception
{
if (args.length !=3)
{
System.out.println("required: <databasename> <userid> <password>");
System.exit(0);
}

String url = "jdbc:db2://localhost:50000/" + args[0];
System.out.println(" Connect to '" + args[0]+ "' database using JCC driver
and type 4 connectivity." );
Class.forName("com.ibm.db2.jcc.DB2Driver");
Connection con = DriverManager.getConnection( url, args[1], args[2]);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select '1',user,current sqlid from sy
sibm.sysdummy1");
while (rs.next())
System.out.println(rs.getString(1) + ' ' + rs.getString(2) + ' ' +
rs.getString(3));
stmt.close();
rs.close();
con.close();
}
}

Jan M. Nelken
Nov 12 '05 #2
Type 4 connections will fail if the db2jd daemon is not active. It is
started by issuing the "db2jstrt" command. You can specify a tcpip port
(db2jstrt port_num) or take the default, 6789. The default can be
changed by setting the DB2JD_PORT_NUMBER registry variable.

Please note that this (db2jstrt) command is not documented in the
command reference (8.2), and I couldn't find it in my master index. The
online information center doesn't have a reference to it either.
Referential documentation can be found in the communications registry
variables. Execute the command with a "-?" parameter and it will tell
you that you can specify the port number.

Phil Sherman

Rhino wrote:
I'm trying to use the JDBC Universal driver to get a Type 4 connection in a
Java client program running under DB2 V8.2.1 but I keep getting a
"connection refused: connect" message.

I'm running DB2 Personal Edition so I am trying to connect to the database
SQLPROCS on this same machine. I've checked the manual and am imitating the
examples as well as I can but the examples are geared to remote machines,
not the local one, so I strongly suspect that the syntax is somehow
different for local machines. Anyway, here is the relevant code:
/**
* Load the JDBC driver.
*/
public void loadDriver() {

String METHOD_NAME = "loadDriver()";

String JDBC_DRIVER_NAME = "com.ibm.db2.jcc.DB2Driver"; //new Universal
JDBC driver

/* Load the JDBC driver. */
try {
Class.forName(JDBC_DRIVER_NAME).newInstance();
}
catch (Exception excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " - Encountered
error " +
"while attempting to load JDBC driver, " + JDBC_DRIVER_NAME + ".");
excp.printStackTrace();
System.exit(16);
}
}

/**
* Get a connection to the database and set Autocommit.
*/
public void connectToDatabase() {

String METHOD_NAME = "connectToDatabase()";

String DB2_URL = "jdbc:db2:sqlprocs"; //old and new Type 2 URL - works
// String DB2_URL =
"jdbc:db2://localhost/sqlprocs:user=rhino;password=rhino;"; //new Type 4
URL - doesn't work
// String DB2_URL =
"jdbc:db2://127.0.0.1/sqlprocs:user=rhino;password=rhino;"; //new Type 4
URL - doesn't work

boolean AUTOCOMMIT = false;

/* Connect to the database. */
try {
conn01 = DriverManager.getConnection(DB2_URL);
}
catch (SQLException sql_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " - Encountered
error while " +
"connecting to URL, " + DB2_URL + ".");
sql_excp.printStackTrace();
System.exit(16);
}

/* Set autocommit. */
try {
conn01.setAutoCommit(AUTOCOMMIT);
}
catch (SQLException sql_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " - Encountered
error while " +
"setting autocommit to " + AUTOCOMMIT + ".");
sql_excp.printStackTrace();
System.exit(16);
}
}
Only the first version of the DB2_URL variable works; the other two don't.
That means that I can get a Type 2 connection but not a Type 4 connection.
What am I doing wrong?

By the way, I can successfully ping both localhost and 127.0.0.1 from my
Windows command prompt so I don't seem to be missing and etc/ entries.

Nov 12 '05 #3
Phil Sherman wrote:
Type 4 connections will fail if the db2jd daemon is not active. It is
started by issuing the "db2jstrt" command. You can specify a tcpip port
(db2jstrt port_num) or take the default, 6789. The default can be
changed by setting the DB2JD_PORT_NUMBER registry variable.

Please note that this (db2jstrt) command is not documented in the
command reference (8.2), and I couldn't find it in my master index. The
online information center doesn't have a reference to it either.
Referential documentation can be found in the communications registry
variables. Execute the command with a "-?" parameter and it will tell
you that you can specify the port number.


1. db2jd applet daemon is not required for JCC Type 4 connections.

2. db2jstrt command is documented in Infoemation center (available online at:
http://publib.boulder.ibm.com/infoce...elp/index.jsp). Just search for
db2jstrt string; you will find that it says:

Connecting with the Type 3 ("net") Driver

To connect with the type 3 driver, first modify the Applt.html file according to
the instructions in the file. Then, start the JDBC applet server on the TCP/IP
port specified in Applt.html. For example, if in Applt.html, you specified param
name=port value='6789', then you would enter:

db2jstrt 6789

Make sure the JDBC port number in the connection string is the recommended
default, "6789". Only change this if you are sure the number does not conflict
with another port number. Do not use the database port number, "50000".
I recommend again to try my test code.

Jan M. Nelken
Nov 12 '05 #4
Thanks for the correction about the difference between type3 and 4
connections.

I still say that this isn't the best place to provide this
documentation. It also implies that you match the port on the server to
what you specify in the applet. I believe that most shops would prefer
to designate the port on the server and have the applets "march to the
server's tune".

Phil Sherman

Jan M. Nelken wrote:

1. db2jd applet daemon is not required for JCC Type 4 connections.

2. db2jstrt command is documented in Infoemation center (available
online at: http://publib.boulder.ibm.com/infoce...elp/index.jsp).
Just search for db2jstrt string; you will find that it says:

Connecting with the Type 3 ("net") Driver

To connect with the type 3 driver, first modify the Applt.html file
according to the instructions in the file. Then, start the JDBC applet
server on the TCP/IP port specified in Applt.html. For example, if in
Applt.html, you specified param name=port value='6789', then you would
enter:

db2jstrt 6789

Make sure the JDBC port number in the connection string is the
recommended default, "6789". Only change this if you are sure the number
does not conflict with another port number. Do not use the database port
number, "50000".
I recommend again to try my test code.

Jan M. Nelken

Nov 12 '05 #5
Phil Sherman wrote:
Thanks for the correction about the difference between type3 and 4
connections.

I still say that this isn't the best place to provide this
documentation. It also implies that you match the port on the server to
what you specify in the applet. I believe that most shops would prefer
to designate the port on the server and have the applets "march to the
server's tune".


Please also see:
"DB2 JDBC Type 3 Driver for Linux, UNIX and Windows (deprecated as of DB2 V8.1)

The DB2 JDBC type 3 driver, also known as the applet or net driver, consists of
a JDBC client and a JDBC server. The DB2 JDBC applet driver can be loaded by a
Web browser along with the applet, or the applet driver can be used in
standalone Java applications. When the applet requests a connection to a DB2
database server, the applet driver opens a TCP/IP socket to the DB2 JDBC applet
server on the machine where the Web server is running. After a connection is set
up, the applet driver sends each of the subsequent database access requests from
the applet to the JDBC server through the TCP/IP connection. The JDBC server
then makes corresponding DB2 calls to perform the task. On completion, the JDBC
server sends the results back to the JDBC client through the connection. The
JDBC server process is db2jd.

The DB2 JDBC Type 3 Driver for Linux, UNIX and Windows will not be supported in
future releases of DB2. You should therefore consider moving to the DB2
Universal JDBC Driver."
Keeping in mind that JDBC type 3 driver will not be supported in future releases
- please also note that port db2jds will be listening on can be changed via
registry variable DB2JD_PORT_NUMBER.

Jan M. Nelken
Nov 12 '05 #6

"Jan M. Nelken" <Un**********@Invalid.Domain> wrote in message
news:Pc********************@rogers.com...
Phil Sherman wrote:
Thanks for the correction about the difference between type3 and 4
connections.

I still say that this isn't the best place to provide this
documentation. It also implies that you match the port on the server to
what you specify in the applet. I believe that most shops would prefer
to designate the port on the server and have the applets "march to the
server's tune".
Please also see:
"DB2 JDBC Type 3 Driver for Linux, UNIX and Windows (deprecated as of DB2

V8.1)
The DB2 JDBC type 3 driver, also known as the applet or net driver, consists of a JDBC client and a JDBC server. The DB2 JDBC applet driver can be loaded by a Web browser along with the applet, or the applet driver can be used in
standalone Java applications. When the applet requests a connection to a DB2 database server, the applet driver opens a TCP/IP socket to the DB2 JDBC applet server on the machine where the Web server is running. After a connection is set up, the applet driver sends each of the subsequent database access requests from the applet to the JDBC server through the TCP/IP connection. The JDBC server then makes corresponding DB2 calls to perform the task. On completion, the JDBC server sends the results back to the JDBC client through the connection. The JDBC server process is db2jd.

The DB2 JDBC Type 3 Driver for Linux, UNIX and Windows will not be supported in future releases of DB2. You should therefore consider moving to the DB2
Universal JDBC Driver."
Keeping in mind that JDBC type 3 driver will not be supported in future releases - please also note that port db2jds will be listening on can be changed via registry variable DB2JD_PORT_NUMBER.

Jan M. Nelken


Thank you both for your help, Jan and Phil. Your code works perfectly, Jan.
Mine works perfectly too now; all I needed to do was add the port number to
the URL that I was using. In other words, I changed:

static final String DB2_URL =
"jdbc:db2://127.0.0.1/sqlprocs:user=rhino;password=rhino;"; //new Type 4
URL - doesn't work
to

static final String DB2_URL =
"jdbc:db2://127.0.0.1:50000/sqlprocs:user=rhino;password=rhino;"; //new Type
4 URL - works
Sorry, I probably should have caught this myself but, for some reason, I was
sure that the port would default to 50000 so I left it out. Obviously, it
DOESN'T default to 50000 as I thought.

Thanks again!

Rhino
Nov 12 '05 #7
aj

Hi Jan:

Notice how in this sample DB2 JDBC java source, and all others,
that the password is hardcoded in the source?

Is there any way around this w/ the DB2 v8 type 4 driver?

My shop has had some security audits recently, and this came up.
I run Java DB2 JDBC programs in batch mode in a cron rather than
interactively. How can I avoid a hardcoded password? Is there
a standard way to do this?

PS - I'm a former Informix DBA, and the Informix type 4 jdbc driver
was sensitive to the contents of the unix hosts.equiv file on the
server. With the correct entries in hosts.equiv, it was not
necessary to specify a password - this is how we previously got
around this. But the DB2 JDBC driver does not act this way.

Any help appreciated.

aj
Jan M. Nelken wrote:
Rhino wrote:
I'm trying to use the JDBC Universal driver to get a Type 4 connection
in a
Java client program running under DB2 V8.2.1 but I keep getting a
"connection refused: connect" message.

I'm running DB2 Personal Edition so I am trying to connect to the
database
SQLPROCS on this same machine. I've checked the manual and am
imitating the
examples as well as I can but the examples are geared to remote machines,
not the local one, so I strongly suspect that the syntax is somehow
different for local machines. Anyway, here is the relevant code:
/**
* Load the JDBC driver.
*/
public void loadDriver() {

String METHOD_NAME = "loadDriver()";

String JDBC_DRIVER_NAME = "com.ibm.db2.jcc.DB2Driver"; //new
Universal
JDBC driver

/* Load the JDBC driver. */
try {
Class.forName(JDBC_DRIVER_NAME).newInstance();
}
catch (Exception excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " - Encountered
error " +
"while attempting to load JDBC driver, " + JDBC_DRIVER_NAME +
".");
excp.printStackTrace();
System.exit(16);
}
}

/**
* Get a connection to the database and set Autocommit.
*/
public void connectToDatabase() {

String METHOD_NAME = "connectToDatabase()";

String DB2_URL = "jdbc:db2:sqlprocs"; //old and new Type 2 URL -
works
// String DB2_URL =
"jdbc:db2://localhost/sqlprocs:user=rhino;password=rhino;"; //new Type 4
URL - doesn't work
// String DB2_URL =
"jdbc:db2://127.0.0.1/sqlprocs:user=rhino;password=rhino;"; //new Type 4
URL - doesn't work

boolean AUTOCOMMIT = false;

/* Connect to the database. */
try {
conn01 = DriverManager.getConnection(DB2_URL);
}
catch (SQLException sql_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " - Encountered
error while " +
"connecting to URL, " + DB2_URL + ".");
sql_excp.printStackTrace();
System.exit(16);
}

/* Set autocommit. */
try {
conn01.setAutoCommit(AUTOCOMMIT);
}
catch (SQLException sql_excp) {
System.err.println(CLASS_NAME + "." + METHOD_NAME + " - Encountered
error while " +
"setting autocommit to " + AUTOCOMMIT + ".");
sql_excp.printStackTrace();
System.exit(16);
}
}
Only the first version of the DB2_URL variable works; the other two
don't.
That means that I can get a Type 2 connection but not a Type 4
connection.
What am I doing wrong?

By the way, I can successfully ping both localhost and 127.0.0.1 from my
Windows command prompt so I don't seem to be missing and etc/ entries.


Try this (watch for wrapped lines):

import java.lang.*;
import java.sql.*;

public class MyJccTest
{
public static void main (String args[]) throws Exception
{
if (args.length !=3)
{
System.out.println("required: <databasename> <userid> <password>");
System.exit(0);
}

String url = "jdbc:db2://localhost:50000/" + args[0];
System.out.println(" Connect to '" + args[0]+ "' database using JCC
driver and type 4 connectivity." );
Class.forName("com.ibm.db2.jcc.DB2Driver");
Connection con = DriverManager.getConnection( url, args[1], args[2]);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select '1',user,current sqlid from sy
sibm.sysdummy1");
while (rs.next())
System.out.println(rs.getString(1) + ' ' + rs.getString(2) + ' ' +
rs.getString(3));
stmt.close();
rs.close();
con.close();
}
}

Jan M. Nelken

Nov 12 '05 #8
aj wrote:

Hi Jan:

Notice how in this sample DB2 JDBC java source, and all others,
that the password is hardcoded in the source?

Is there any way around this w/ the DB2 v8 type 4 driver?


Notice that in my yesy program - userid and password was not hardcoded at source
but supplied as argument:

Connection con = DriverManager.getConnection( url, args[1], args[2]);

which of course does not help you if you are talking about script submitted via
cron.

V8.2 adds plugin authentication - so you can code almost everything in your
security plugin in order to authenticate.

Please also note that local connection to database does not require password -
it assumes userid of already logged user. You may want to experiment with rcmd
and/or rlogin to achieve that.

Jan M. Nelken
Nov 12 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ed Trubia | last post by:
Have the following type that applies contraints. How could this be easily coded in Java type ET_ACTIVE_PORT_CONFIGURATION is ( HDX_TX1_RX1, HDX_TX2_RX2, HDX_TX1_RX2, HDX_TX2_RX1,...
73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
5
by: Maurice Ling | last post by:
Hi, I have read that this had been asked before but there's no satisfactory replies then. I have a module (pA) written in python, which originally is called by another python module (pB), and...
0
by: Michael Welp | last post by:
Hallo NG, i have a problem with a Timestamp field. I use a JDBC Type 4 - Driver. When i make a select on a table with a timestamp-field i got for example the result : 2004-04-30 15:23:44.0 ...
5
by: Joe Kinsella | last post by:
Java JDBC defines four types of database drivers. A type 4 driver is a native protocol, fully Java-enabled driver. For Oracle, this means the driver is written to the socket based protocol...
5
by: Guru | last post by:
public String generateSP() { String s = ""; Iterator iterator = hmTable.keySet().iterator(); String s1 = ""; while(iterator.hasNext()) { String s2 = (String)iterator.next(); s = s.length() !=...
3
by: delirman49 | last post by:
Hello, Sorry for my english. This is my problem : I must create for a great french company a webmethod with 2 "in" parameters. The first is an object, no problem. The second must be a...
6
by: sapsi | last post by:
Hello, I am using HadoopStreaming using a BinaryInputStream. What this basically does is send a stream of bytes ( the java type is : private byte bytes) to my python program. I have done a...
0
by: michellebong | last post by:
Dear experts, I have question on how to add my java type dll to vb.net 2005 as reference. The error output : A reference to '<..>.dll' could not be added. Please make sure that the file is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.