package db; import javax.swing.*; import javax.swing.table.*; import java.sql.*; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author culikzde */ public class Window extends javax.swing.JFrame { /** * Creates new form Window */ public Window() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { jButton1 = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("jButton1"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jTable1.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null} }, new String [] { "Title 1", "Title 2", "Title 3", "Title 4" } )); jScrollPane1.setViewportView(jTable1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jButton1) .addGap(0, 0, Short.MAX_VALUE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)) ); pack(); }// //GEN-END:initComponents // import javax.swing.table.*; DefaultTableModel m; private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed if (m == null) { m = new DefaultTableModel(); jTable1.setModel (m); } // String [] data = { "abc", "def", "klm" }; // m.addRow (data); // Services / Java DB / Start Server // Project / Libraries / Add JAR // c:/Program Files/Java/jdk_1.8.0_25/db/lib/derbyCLIENT.jar // import java.sql.*; String host = "jdbc:derby://localhost:1527/db"; String user = "app"; String password = "app"; try { Connection conn = DriverManager.getConnection (host, user, password); Statement stat = conn.createStatement (); stat.execute ("drop table colors"); stat.execute ("create table colors " + "(name varchar(80), red int, green int, blue int)"); stat.execute ("insert into colors values" + "('modra', 0, 0, 255)"); PreparedStatement p = conn.prepareStatement ("insert into colors values (?, ?, ?, ?)"); p.setString (1, "blede modra"); p.setInt (2, 128); p.setInt (3, 128); p.setInt (4, 255); p.execute (); p.setString (1, "blede zelena"); p.setInt (3, 255); p.setInt (4, 128); p.execute (); ResultSet rs = stat.executeQuery ("select * from colors"); int cnt = rs.getMetaData().getColumnCount(); m.setColumnCount (cnt); String [] names = new String [cnt]; for (int i = 1; i <= cnt; i++) names [i-1] = rs.getMetaData().getColumnName(i); m.setColumnIdentifiers (names); while (rs.next ()) { String [] data = new String [cnt]; for (int i = 1; i <= cnt; i++) data[i-1] = rs.getString (i); m.addRow (data); // m.addRow (new String [] { rs.getString ("name") } ); } } catch (SQLException ex) { setTitle (""+ex); } }//GEN-LAST:event_jButton1ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Window().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable1; // End of variables declaration//GEN-END:variables }