import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* -- Use try-resource to auto close the resources.
*
*/
public class RetrieveTableContents
{
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 connection = DriverManager.getConnection
("jdbc:derby://localhost:143/testDb","username",
"password");
try( Statement stmt = connection.createStatement()){
ResultSet rs = stmt.executeQuery
("SELECT * FROM Table_name");
System.out.println("id name job");
while (rs.next()) {
int id = rs.getInt("id");
String col1 = rs.getString("ColName");
String col2 = rs.getString("ColName");
System.out.println(id+" "+col1+" "+col2);
}
}
}
catch(SQLException e){
System.out.println("SQL exception occured" + e);
}
}
}
No comments:
Post a Comment