connecting to DB2 [message #183630] |
Sun, 10 December 2006 02:59 |
Eclipse User |
|
|
|
Originally posted by: tmmuralikrishnan.gmail.com
Hey pals.. im new to eclipse.. i want to get connected to database DB2..
please mention me the procedures or give me a sample code for connecting
to DB@
|
|
|
Re: connecting to DB2 [message #184277 is a reply to message #183630] |
Tue, 12 December 2006 18:57 |
Eclipse User |
|
|
|
Originally posted by: chadboomershine.xxxxxxx.net
import java.sql.*;
class DB2Sample {
static {
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String argv[]) {
Connection con = null;
// URL is jdbc:db2:dbname
String url = "jdbc:db2:xxxxxx";
try {
if (argv.length == 0) {
// connect with default id/password
con = DriverManager.getConnection(url);
}
else if (argv.length == 2) {
String userid = argv[0];
String passwd = argv[1];
// connect with user-provided username and password
con = DriverManager.getConnection(url, userid, passwd);
}
else {
System.out.println("Usage: java DB2Sample [username
password]");
System.exit(0);
}
// retrieve data from the database
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery
("select * from ...");
System.out.println("Output:");
System.out.println(" ");
// display the result set
// rs.next() returns false when there are no more rows
while (rs.next()) {
String column1 = rs.getString("column1");
String column2 = rs.getString("column2");
System.out.println(" " + column1 + " " + column2);
}
rs.close();
stmt.close();
} catch( Exception e ) {
System.out.println(e);
}
}
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.08392 seconds