Thursday, April 10, 2008

JDBC : Easy Steps

hi all,
this is a sample program to show the jdbc connectivity( very simple and limited scopes but enough to create any program using jdbc)

Connection conn;
String driver = "XXXXXX.xxx.xx.xx";
String url = "xxx:XXX:xxx"; // remember the colon here
// url contains the database name

// loading the driver
Class.forName(driver);

// creating a connection
conn = DriverManager.getConnection(url);

Statement stmt = conn.createStatement();
String query = "xxxxxxx";

ResultSet result = null;

/*
* different queries need different methods to be executed
*/

/*
*
* in case of select
* result = stmt.executeQuery(query);
*
*/

/*
*
* in case of insert, update, delete
* stmt.executeUpdate(query);
*
*/

// Like if we use select query
result = stmt.executeQuery(query);

// initially curson is set before first row of the result
// so we need to make it at the desired row

result.next();
// we can use result.first() to get it at first row or result.last() to get it at the
// last row

String str = result.getString("attribute-name");

// finally
conn.close();

this was the very basic tute to implement jdbc.
if u want more then use javadocs.

thanks
Ravi Kumar Gupta ~A.K.A~ "D'Maverick"

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.