Java examples for JDBC:Derby
Connect to Derby database and select data
import java.sql.*; import javax.swing.*; public class PlanTrip extends javax.swing.JFrame { Connection con;/* w w w .j a va 2 s . c o m*/ PreparedStatement stmt; ResultSet rs; private void DoConnect() { try { String host = "jdbc:derby://localhost:1527/TripprApp"; String uName = "Trip"; String uPass = "vt"; Connection con = DriverManager .getConnection(host, uName, uPass); String one = "'" + (String) FromCombo.getSelectedItem() + "'"; String two = "'" + (String) ToCombo.getSelectedItem() + "'"; String sql = "Select * from bus natural join stops where FROMSTOP=" + one + "and TOSTOP=" + two; stmt = con.prepareStatement(sql); rs = stmt.executeQuery(); FromTo.setModel(DbUtils.resultSetToTableModel(rs)); // TODO code application logic here } catch (SQLException err) { System.out.println(err.getMessage()); } } private void fromCombo() { try { String host = "jdbc:derby://localhost:1527/TripprApp"; String uName = "Trip"; String uPass = "vt"; Connection con = DriverManager .getConnection(host, uName, uPass); String sql = "Select * from stops"; stmt = con.prepareStatement(sql); rs = stmt.executeQuery(); while (rs.next()) { int flag = 0; String name = rs.getString("FROMSTOP"); int i = FromCombo.getItemCount(); while (i > 0) { if (name.equals((String) FromCombo.getItemAt(i))) flag = 1; i--; } if (flag == 0) FromCombo.addItem(name); } } catch (SQLException err) { System.out.println(err.getMessage()); } } private void toCombo() { try { String host = "jdbc:derby://localhost:1527/TripprApp"; String uName = "Trip"; String uPass = "vt"; Connection con = DriverManager .getConnection(host, uName, uPass); String sql = "Select * from stops"; stmt = con.prepareStatement(sql); rs = stmt.executeQuery(); while (rs.next()) { int flag = 0; String name = rs.getString("TOSTOP"); int i = ToCombo.getItemCount(); while (i > 0) { if (name.equals((String) ToCombo.getItemAt(i))) flag = 1; i--; } if (flag == 0) ToCombo.addItem(name); } } catch (SQLException err) { System.out.println(err.getMessage()); } }