• September 28, 2014
  • JDBC
  • Comments Off on JDBC PreparedStatement select records example

JDBC PreparedStatement select records example

The JDBC PreparedStatement is used to execute parameterized queries against the database. Let us study JDBC PreparedStatement by select records example.

Example:

JDBCTest.java

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.javawithease.util.JDBCUtil;
 
/**
 * This class is used to select a list of records from DB table
 * using PreparedStatement.
 * @author javawithease
 */
public class JDBCTest {
	public static void main(String args[]){
		Connection conn = null;
		PreparedStatement preparedStatement = null;
 
		String query = "select EMPLOYEE_ID, NAME from EMPLOYEE";
 
		try{			
			//get connection
			conn = JDBCUtil.getConnection();
 
			//create preparedStatement
			preparedStatement = conn.prepareStatement(query);
 
			//execute query
			ResultSet rs = 
                                  preparedStatement.executeQuery(query);
			while (rs.next()) {				 
			   String empId = rs.getString("EMPLOYEE_ID");
			   String empName = rs.getString("NAME");
 
			   System.out.println("EmpId : " + empId);
			   System.out.println("EmpName : " + empName); 
			}
 
			//close connection
			preparedStatement.close();
			conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}	
}

JDBCUtil.java

import java.sql.Connection;
import java.sql.DriverManager;
 
/**
 * This is a utility class for JDBC connection.
 * @author jawithease
 */
public class JDBCUtil {
	//JDBC and database properties.
	private static final String DB_DRIVER = 
		           "oracle.jdbc.driver.OracleDriver";
	private static final String DB_URL = 
		        "jdbc:oracle:thin:@localhost:1521:XE";
	private static final String DB_USERNAME = "system";
	private static final String DB_PASSWORD = "oracle";
 
	public static Connection getConnection(){
		Connection conn = null;
		try{
			//Register the JDBC driver
			Class.forName(DB_DRIVER);
 
			//Open the connection
			conn = DriverManager.
			getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
 
			if(conn != null){
			   System.out.println("Successfully connected.");
			}else{
			   System.out.println("Failed to connect.");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}	
}

Output:

Successfully connected.
EmpId : 7
EmpName : Harish Yadav
EmpId : 2
EmpName : vivek
EmpId : 3
EmpName : Himanshu
EmpId : 1
EmpName : Bharat
EmpId : 4
EmpName : Bharti
EmpId : 8
EmpName : Abhishek Rathor
EmpId : 11
EmpName : Harish Kansal
EmpId : 12
EmpName : Vivek Solenki

Download this example.
 
Next Topic: JDBC PreparedStatement deletes a record example.
Previous Topic: JDBC PreparedStatement updates a record example.

Related Topics:

JDBC PreparedStatement creates a table example.
JDBC PreparedStatement inserts a record example.
JDBC PreparedStatement updates a record example.
JDBC PreparedStatement select records example.
JDBC PreparedStatement deletes a record example.
JDBC PreparedStatement batch update example.

 


Comments are closed.