using jdbc to connect to db2

R

r_stringer66

Hi all,

I am pretty new to JDBC and I am trying to write a simple application
to connect to a db2 database residing on a different machine.

I am looking for a code snippet and the neceesary steps in order to
establish the connection to the database and to send some simple
"SELECT" statements to the database.

I would really appreciate if somone could point me to the right
direction in this regards.

Thanks,
Rob
 
J

jeak110

/*## loading a connector */
Connection connection;
Statement statement;
ResultSet rs;
try{
Class.forName(jdbc_driver_name).newInstance();
} catch(InstantiationException e){
logger.info("<error type=\""+e.toString()+"\"
discription=\""+e.getMessage()+"\" />");
return null;
} catch(ClassNotFoundException e){
logger.info("<error type=\""+e.toString()+"\"
discription=\""+e.getMessage()+"\" />");
return null;
} catch(IllegalAccessException e){
logger.info("<error type=\""+e.toString()+"\"
discription=\""+e.getMessage()+"\" />");
return null;
}

/*## connect to database */
try{
connection =
DriverManager.getConnection(dblink_2_ss_database, login2db, pass2db);
} catch(SQLException e){
logger.info("<error type=\""+e.toString()+"\"
discription=\""+e.getMessage()+"\" />");
return "<error type=\""+e.toString()+"\"
discription=\""+e.getMessage()+"\" />";
}

/*## connect to database */
String dkstr;
try{
statement = connection.createStatement();
rs = statement.executeQuery("select q.arendator,q.no_dog,
q.address,q.date_begin, q.date_end, q.position_fc, q.telephone_fc from
arenda.vdog_ar q where sysdate between q.date_begin and q.date_end");

rs2xml j = new rs2xml();
j.setWithoutMDFlag(true);
dkstr = j.writeXML(rs);
statement.close();
connection.close();
} catch(SQLException e){
logger.info("<error type=\""+e.toString()+"\"
discription=\""+e.getMessage()+"\" />");
return "<error type=\""+e.toString()+"\"
discription=\""+e.getMessage()+"\" />";
}
return dkstr;

// it's me code!
there is a good book ... John Wiley & Sons - MySQL and Java Developer's
Guide or O'Reilly Java Programming with Oracle JDBC. I dont remember
where I've downloaded it. but I think you can find they here
(ebiku.powernews.ru)

(e-mail address removed) пиÑал(а):
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top