/* * InsertTable.java * * Created on 2005/01/17, 15:49 */ package mysqlop; import java.sql.*; import java.util.ArrayList; /** * * @author noniko */ public class InsertTable extends javax.swing.JFrame { /** Creates new form InsertTable */ public InsertTable() throws Exception{ mdbc=new MyDBConnection(); mdbc.init(); Connection conn=mdbc.getMyConnection(); stmt= conn.createStatement(); 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. */ private void initComponents() {//GEN-BEGIN:initComponents dataPanel = new javax.swing.JPanel(); idLabel = new javax.swing.JLabel(); idField = new javax.swing.JTextField(); nameLabel = new javax.swing.JLabel(); nameField = new javax.swing.JTextField(); vendorLabel = new javax.swing.JLabel(); vendorField = new javax.swing.JTextField(); typeLabel = new javax.swing.JLabel(); typeCombo = new javax.swing.JComboBox(); tablePane = new javax.swing.JScrollPane(); carTable = new javax.swing.JTable(); sendPanel = new javax.swing.JPanel(); sendButton = new javax.swing.JButton(); commentLabel = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { formWindowClosing(evt); } }); dataPanel.setLayout(new java.awt.GridLayout(4, 2, 1, 2)); idLabel.setText("ID:"); dataPanel.add(idLabel); dataPanel.add(idField); nameLabel.setText("Name:"); dataPanel.add(nameLabel); dataPanel.add(nameField); vendorLabel.setText("Vendor:"); dataPanel.add(vendorLabel); dataPanel.add(vendorField); typeLabel.setText("Type:"); dataPanel.add(typeLabel); typeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "FF", "FR", "MR" })); dataPanel.add(typeCombo); getContentPane().add(dataPanel, java.awt.BorderLayout.NORTH); ResultSet rs=getResultFromCars(); carTable.setModel(new CarTableModel(rs)); mdbc.close(rs); tablePane.setViewportView(carTable); getContentPane().add(tablePane, java.awt.BorderLayout.SOUTH); sendPanel.setLayout(new java.awt.GridLayout(2, 0)); sendButton.setText("Send"); sendButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { sendButtonActionPerformed(evt); } }); sendPanel.add(sendButton); commentLabel.setText("Click button to send data"); sendPanel.add(commentLabel); getContentPane().add(sendPanel, java.awt.BorderLayout.CENTER); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); setBounds((screenSize.width-400)/2, (screenSize.height-600)/2, 400, 600); }//GEN-END:initComponents private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed // TODO add your handling code here: String carid=idField.getText(); String name=nameField.getText(); String vendor=vendorField.getText(); String type=(String)typeCombo.getSelectedItem(); String insertStr=""; try{ insertStr="insert into cars (carid, name, vendor, type) values(" +quotate(carid)+"," +quotate(name)+"," +quotate(vendor)+"," +quotate(type) +")"; int done=stmt.executeUpdate(insertStr); commentLabel.setText("1 row inserted"); getContentPane().removeAll(); initComponents(); } catch(Exception e){ commentLabel.setText("Error occurred in inserting data"); e.printStackTrace(); } }//GEN-LAST:event_sendButtonActionPerformed private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing // TODO add your handling code here: mdbc.close(stmt); mdbc.destroy(); }//GEN-LAST:event_formWindowClosing /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { try{ new InsertTable().setVisible(true); } catch(Exception e){ } } }); } public ResultSet getResultFromCars() { ResultSet rs=null; try{ rs=stmt.executeQuery("Select * from cars"); } catch(SQLException e){} return rs; } public String quotate(String content){ return "'"+content+"'"; } private MyDBConnection mdbc; private java.sql.Statement stmt; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTable carTable; private javax.swing.JLabel commentLabel; private javax.swing.JPanel dataPanel; private javax.swing.JTextField idField; private javax.swing.JLabel idLabel; private javax.swing.JTextField nameField; private javax.swing.JLabel nameLabel; private javax.swing.JButton sendButton; private javax.swing.JPanel sendPanel; private javax.swing.JScrollPane tablePane; private javax.swing.JComboBox typeCombo; private javax.swing.JLabel typeLabel; private javax.swing.JTextField vendorField; private javax.swing.JLabel vendorLabel; // End of variables declaration//GEN-END:variables }