Powered by Blogger.

3.Interaction with RDBMS

>> Saturday, March 19, 2011

These are the General steps to follow  while usingn JDBC.

1) Load the RDBMS specific JDBC driver because this driver actually communicates with the database.
2) Open the connection to database which is then used to send SQL statements and get results back.
3) Create JDBC Statement object. This object contains SQL query.
4) Execute statement which returns resultset(s). ResultSet contains the tuples of database table as a result of SQL query.
5) Process the result set.
6) Close the connection.

Example: Retrieve list of employees from Employee table using JDBC.

String url = “jdbc:odbc:” + dbName;

List employeeList = new ArrayList();

//load the jdbc-odbc driver 

class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

//Open a connection to database

Connection con = DriverManager.getConnection(url);

//create Statement object

Statement stmt = con.createStatement();

//execute statement
ResultSet rs = stmt.executeQuery("SELECT * FROM Sells"); while ( rs.next() ){
  EmployeeBean eb = new Employeebean(); 
  eb.setName(rs.getString("name")); 
  eb.setSalary(rs.getFloat("salary")); 
  employeeList.add(eb);
}
Reference Books:

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2022. All rights reserved.