Sunday, 17 July 2016

JDBC Statement – Select list of the records

package net.dharmaraj;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import net.dharmaraj.JDBCStatementCreateExample;
public class JDBCStatementSelectExample {
public static void main(String[] args) {
try {
selectRecordsFromDbUserTable();
} catch (Exception e) {
System.out.println(e.getMessage());
}

}//end of Main Method
private static void selectRecordsFromDbUserTable() throws SQLException {

Connection dbConnection = null;
Statement statement = null;

String selectTableSQL = "SELECT USER_ID, USERNAME from DBUSER";

try {
dbConnection = JDBCStatementCreateExample.getDBConnection();
statement = dbConnection.createStatement();
System.out.println(selectTableSQL);
ResultSet rs = statement.executeQuery(selectTableSQL);
while (rs.next()) {

String userid = rs.getString("USER_ID");
String username = rs.getString("USERNAME");

System.out.println("userid : " + userid);
System.out.println("username : " + username);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (statement != null) {
statement.close();
}

if (dbConnection != null) {
dbConnection.close();
}
}//end of try catch block

}
}//end of Class

No comments:

Post a Comment