Recent Posts

Saturday, 10 November 2018

JDBC in Simple Way

1. Driver
     To convert Java specific calls into Database specific calls and Database specific calls into Java calls.
2. Connection
     By using Connection, Java Application can communicate with Database.

3. Statement
     By using Statement Object we can send our SQL Query to the Database and we can get Results from Database.

4. ResultSet
     ResultSet holds Results of SQL Query.

Steps for JDBC Application
1. Load and Register Driver
2. Establish Connection between Java Application and Database
3. Create Statement Object
4. Send and Execute SQL Query
5. Process Results from ResultSet
6. Close Connection
import java.sql.*; 

/**
 * 
 * @author ashok.mariyala
 *
 */
public class JdbcDemo { 
   public static void main(String[] args) throws Exception { 
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
      Connection con=DriverManager.getConnection("jdbc:odbc:demodsn","scott","tiger"); 
      Statement st = con.createStatement(); 
      ResultSet rs= st.executeQuery("select * from employees"); 
      while(rs.next()) { 
         System.out.println(rs.getInt(1)+".."+rs.getString(2)+".."+rs.getDouble(3)+"..."+rs.getString(4)); 
      }
      con.close(); 
   }
}

Next Tutorial : Storage Areas

Previous Tutorial : JDBC Introduction 
 

No comments:

Post a Comment