Problem Description:: How To Retrieve Contents of A Table Using JDBC Connection?

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Java Examples - Retrieve Table Contents

1 of 1

http://www.tutorialspoint.com/cgi-bin/printpage.cgi

http://www.tutorialspoint.com/javaexamples/jdbc_resultset.htm

Copyright tutorialspoint.com

Problem Description:
How to retrieve contents of a table using JDBC connection?

Solution:
Following example uses getString,getInt & executeQuery methods to fetch & display the contents of the table.
import java.sql.*;
public class jdbcResultSet {
public static void main(String[] args) {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
}
catch(ClassNotFoundException e) {
System.out.println("Class not found "+ e);
}
try {
Connection con = DriverManager.getConnection
("jdbc:derby://localhost:1527/testDb","username",
"password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery
("SELECT * FROM employee");
System.out.println("id name
job");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String job = rs.getString("job");
System.out.println(id+"
"+name+"
"+job);
}
}
catch(SQLException e){
System.out.println("SQL exception occured" + e);
}
}
}

Result:
The above code sample will produce the following result.The result may vary.
id
1
2

name
alok
ravi

job
trainee
trainee

21-Jun-16 4:31 PM

You might also like