Query Derby database - Java JDBC

Java examples for JDBC:ResultSet

Description

Query Derby database

Demo Code

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

public class Main {
  public static void main(String[] arguments) {
    String data = "jdbc:derby://localhost:1527/sample";
    try (Connection conn = DriverManager.getConnection(data, "app", "APP");
        Statement st = conn.createStatement()) {

      Class.forName("org.apache.derby.jdbc.ClientDriver");

      ResultSet rec = st.executeQuery("select CUSTOMER_ID, NAME, CITY, STATE "
          + "from APP.CUSTOMER " + "order by CUSTOMER_ID");
      while (rec.next()) {
        System.out.println("CUSTOMER_ID:\t" + rec.getString(1));
        System.out.println("NAME:\t" + rec.getString(2));
        System.out.println("CITY:\t" + rec.getString(3));
        System.out.println("STATE:\t" + rec.getString(4));
        System.out.println();/*w ww  .j  a  va 2  s. com*/
      }
      st.close();
    } catch (SQLException s) {
      System.out.println("SQL Error: " + s.toString() + " " + s.getErrorCode()
          + " " + s.getSQLState());
    } catch (Exception e) {
      System.out.println("Error: " + e.toString() + e.getMessage());
    }
  }
}

Result


Related Tutorials