JDBC: Select Records Example


 

JDBC: Select Records Example

In this section, you will learn how to select records from the table using JDBC API.

In this section, you will learn how to select records from the table using JDBC API.

JDBC: Select Records Example

In this section, you will learn how to select records from the table using JDBC API.

Select Records : Select statement retrieves data from the table and display to the console. Data are retrieved in the ResultSet  object and by using print statement you can display it into the console or also use it for another operations.

For selecting table records we need to understand some terms which are important in creation of database connection and performing different operations.

Connection: This  interface specifies connection with specific databases like: MySQL, Ms-Access, Oracle etc and java files. The SQL statements are executed within the context of this interface.

Class.forName(String driver): It loads the driver.

DriverManager  :  The DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property

getConnection(String url+dbName, String userName, String password): This method establishes a connection to specified database url.

The MySQL connection URL has the following format:
jdbc:mysql://[host][:port]/[database][?property1][=value1]..

  • host :The host name where MySQL server is running. Default is 127.0.0.1 - the IP address of localhost.
  • port : The port number where MySQL is listening for connection. Default is 3306.
  • dbName : The name of an existing database on MySQL server. If not specified, the connection starts no current database.
  • Property : The name of a supported connection properties. "userName" and "password" are 2 most important properties.

Statement: This interface executes the SQL statement and returns the result it produces.

createStatement(): It is a method of Connection interface which returns Statement object.

executeQuery(String sql): Executes the given SQL statement, which returns a single ResultSet object.

Example : In this example we are displaying record of student table by using select statement. First establish connection to the database then write select query  and call executeQuery(sql).

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

class SelectRecords {
public static void main(String[] args) {
System.out.println("Select Records Example...");
Connection con = null;
Statement statement = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "students";
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driverName);

// Connecting to the database
con = DriverManager.getConnection(url + dbName, userName, password);
try {
statement = con.createStatement();

// Selecting records
String sql = "SELECT * FROM student";
rs = statement.executeQuery(sql);
System.out.println("RollNo\tName\tCourse");
while (rs.next()) {
int roll = rs.getInt("roll_no");
String name = rs.getString("name");
String course = rs.getString("course");
System.out.println(roll + "\t" + name + "\t" + course);

}

} catch (SQLException e) {
System.out.println("Table doesn't exist.");
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Output :

Select Records Example...
RollNo	Name	Course
----------------------
1	Roxi	MCA
2	Mandy	BCA
3	Rose	MCA

Ads