javapandit.net
  • Home
  • Quick Java
    • Exception Handling
    • Collections Framework
  • Java Best Practices
  • Web Services
    • Web Service Basics
    • Ten Basic webservice concepts
    • XML
    • Apache Axis
    • Restful Web Services
  • JMS Concepts
    • JMS- MySQL
  • Hadoop
    • NoSQL DATABASEs
    • Apache Sqoop
    • Hadoop Interview Questions
  • Java 5
  • Java 8
    • Java 8 : Lambda Expressions
  • JDBC
  • Java Architect
    • Enterprise application re-platforming strategies
    • Java Memory Management
  • Java Programs
  • Technical Tips
    • How to set JAVA_HOME environment variable
    • How to create an auto increment field in Oracle?
    • Linux Commands
  • Best Java Interview Questions
    • Java Interview Questions- YouTube
  • Interview Questions
    • Java Tech interview Questions
    • Core Java Interview Questions >
      • core tech questions1
      • Java Collection interview questions
      • Java Concurrency
    • Servlets Interview Questions
    • JSP Interview Questions
    • Java Web Services Interview Questions
    • EJB Interview Questions
    • XML Interview Questions
    • JMS Interview Questions
  • Struts Interview Questions
    • Struts 2 Interview Questions
  • Java EE Architects Interview Questions
    • Java Architect Interview Questions
    • Top 10 reasons for Java Enterprise Application Performance Problems
    • Web Application Scalability Questions for IT Architect
  • JavaPandit's Blog
  • Web Services Interview Questions
  • Servlets And JSP
  • Oracle SOA Interview Questions
    • Open ESB /JBI
    • BPEL Language
  • Log4J
  • Ant
  • Maven
  • JMeter
  • JUnit
  • Apache POI Framework
  • ORCALE SERVICE BUS (OSB) Interview Questions
  • J2EE Patterns
    • Model-View-Controller (MVC)
    • Front Controller
    • DAO
    • Business Delegate
    • Session Facade
    • Service Locator
    • Transfer Object
    • Design Patterns >
      • Creational Patterns >
        • Singleton
      • Behavioural Patterns
      • Structural Patterns
    • Intercepting Filter
  • SQL Interview Questions/Lab
  • Best Wall Papers
    • Devotional Songs
  • Java Community
  • HIBERNATE
  • ORACLE CC&B
    • Oracle CC&B Interview Questions
  • Docker
  • Little Princess
    • Sai Tanvi Naming Ceremony Celebrations
    • Rice Feeding Ceremony
    • Sai Tanvi Gallery
  • APPSC Career Guidance
    • AP History
    • Indian Polity
    • Indian Economy
    • Science & Technology
    • Mental Ability and Reasoning
    • Disaster Management
    • Current Affairs and Events
    • General Sciences >
      • Biology
      • Physics
      • Chemistry
    • Previous Question Papers
  • About Us
  • Contact US
5. Extract data from result set:
     Statement interface defines methods that are used to interact with database via the execution of SQL statements. The Statement class has three methods for executing statements:
executeQuery(), executeUpdate(), and execute(). For a SELECT statement, the method to use is executeQuery . For statements that create or modify tables, the method to use is executeUpdate. Note: Statements that create a table, alter a table, or drop a table are all examples of DDL
statements and are executed with the method executeUpdate. execute() executes an SQL
statement that is written as String object.
ResultSet provides access to a table of data generated by executing a Statement. The table rows are retrieved in sequence. A ResultSet maintains a cursor pointing to its current row of data. The next() method is used to successively step through the rows of the tabular results.
ResultSetMetaData Interface holds information on the types and properties of the columns in a ResultSet. It is constructed from the Connection object.
6. Clean up the environment:
    Closing database connection 
           try{
                // code
            }finally{
            connection.close();
            }
Sample Example:
Pre-Requisite: 
    You need to have good understanding on the following two subjects to learn JDBC:
  1. Core JAVA Programming
  2. SQL or MYSQL Database
JDBC - Environment Setup: 
    Make sure you have done following setup:
  1. Core JAVA Installation
  2. SQL or MySQL Database Installation
Apart from the above you need to setup a database which you would use for your project. Assuming this is EMP and you have created on table Employees within the same database.
//STEP 1. Import required packages
import java.sql.*;
public class FirstExample { 
    // JDBC driver name and database URL 
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
    static final String DB_URL = "jdbc:mysql://localhost/EMP"; 
    // Database credentials 
    static final String USER = "username"; 
    static final String PASS = "password"; 
    public static void main(String[] args) { 
        Connection conn = null; 
        Statement stmt = null; 
        try{ 
                //STEP 2: Register JDBC driver 
                Class.forName("com.mysql.jdbc.Driver");
                 //STEP 3: Open a connection 
                System.out.println("Connecting to database..."); 
                conn = DriverManager.getConnection(DB_URL,USER,PASS); 
                 //STEP 4: Execute a query 
                System.out.println("Creating statement...");
                stmt = conn.createStatement(); 
                String sql; sql = "SELECT id, first, last, age FROM Employees"; 
                ResultSet rs = stmt.executeQuery(sql); 
                //STEP 5: Extract data from result set 
                while(rs.next()){ 
                    //Retrieve by column name
                     int id = rs.getInt("id"); 
                     int age = rs.getInt("age"); 
                     String first = rs.getString("first"); 
                     String last = rs.getString("last"); 
                    //Display values 
                    System.out.print("ID: " + id); 
                    System.out.print(", Age: " + age); 
                    System.out.print(", First: " + first); 
                    System.out.println(", Last: " + last);
                 } 
               
Prev 1 2 3 4 Next
Powered by Create your own unique website with customizable templates.