Jump to content
xisto Community

dcalladi

Members
  • Content Count

    1
  • Joined

  • Last visited

Everything posted by dcalladi

  1. i have a JTable which data is called form a SQL database when a button is pressed i take some variable from a combobox and a textfield for the SQL query. it works the first time and loads up what i want correctly then when i click a record it should fill some textfields with information this also works but when i try to do a new search i get pushed to my catch as this is all in a try and catch loop. a Optionpane comes up and i click ok and it carriers on but the old information is not replaced. when i click on the old information though the new one comes up and the textfields are filled correctly. here is my code any help would be greatly appreciated package possystem;import javax.swing.JOptionPane;import javax.swing.table.*;import java.util.Vector;import java.sql.*;import javax.swing.ListSelectionModel;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.event.TableModelEvent;import javax.swing.table.AbstractTableModel;/* * DatabaseItemForm.java * * Created on 19-Oct-2011, 19:06:28 *//** * * @author Owner */public class DatabaseItemForm extends javax.swing.JFrame { ListSelectionModel listSelectionModel; /** Creates new form DatabaseItemForm */ public DatabaseItemForm() { 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") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); searchResultsScrollpane = new javax.swing.JScrollPane(); searchResults = new javax.swing.JTable(); searchByLB = new javax.swing.JLabel(); searchByCB = new javax.swing.JComboBox(); searchByTF = new javax.swing.JTextField(); searchByBT = new javax.swing.JButton(); barcodeLB = new javax.swing.JLabel(); barcodeTF = new javax.swing.JTextField(); nameLB = new javax.swing.JLabel(); nameTF = new javax.swing.JTextField(); costLB = new javax.swing.JLabel(); costTF = new javax.swing.JTextField(); comfirmBT = new javax.swing.JButton(); closeBT = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); searchResults.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 [] { "Id", "Barcode", "Name", "Cost" } ) { boolean[] canEdit = new boolean [] { false, false, false, false }; public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit [columnIndex]; } }); searchResults.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF); searchResults.setAutoscrolls(false); searchResults.setFillsViewportHeight(true); searchResults.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); searchResults.setShowHorizontalLines(false); searchResultsScrollpane.setViewportView(searchResults); searchResults.getColumnModel().getColumn(0).setResizable(false); searchResults.getColumnModel().getColumn(0).setPreferredWidth(50); searchResults.getColumnModel().getColumn(1).setResizable(false); searchResults.getColumnModel().getColumn(1).setPreferredWidth(100); searchResults.getColumnModel().getColumn(2).setResizable(false); searchResults.getColumnModel().getColumn(2).setPreferredWidth(200); searchResults.getColumnModel().getColumn(3).setResizable(false); searchResults.getColumnModel().getColumn(3).setPreferredWidth(100); searchByLB.setText("Search By:"); searchByCB.setEditable(true); searchByCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Please Choose", "Barcode", "Name", "Cost" })); searchByBT.setText("Search"); searchByBT.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { searchByBTActionPerformed(evt); } }); barcodeLB.setText("Please Enter Barcode"); nameLB.setText("Please Enter Name"); costLB.setText("Please Enter Cost"); comfirmBT.setText("Comfirm Changes"); comfirmBT.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { comfirmBTActionPerformed(evt); } }); closeBT.setText("Close"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(21, 21, 21) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(searchByLB) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(searchByCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(searchByTF, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(searchByBT)) .addComponent(nameLB) .addComponent(costLB) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(barcodeTF, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(nameTF, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(costTF, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(barcodeLB, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 222, Short.MAX_VALUE) .addComponent(comfirmBT))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 190, Short.MAX_VALUE)) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(searchResultsScrollpane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))) .addComponent(closeBT) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(closeBT)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup() .addGap(22, 22, 22) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(searchByLB) .addComponent(searchByCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(searchByTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(searchByBT)) .addGap(18, 18, 18) .addComponent(barcodeLB) .addGap(18, 18, 18) .addComponent(barcodeTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(nameLB) .addGap(18, 18, 18) .addComponent(nameTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(costLB) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(costTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(comfirmBT)) .addGap(18, 18, 18) .addComponent(searchResultsScrollpane, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE))) .addContainerGap()) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); }// </editor-fold> private void searchByBTActionPerformed(java.awt.event.ActionEvent evt) { String searchType = (String) searchByCB.getSelectedItem(); String searchText = searchByTF.getText(); tableFiller(searchType, searchText); } private void comfirmBTActionPerformed(java.awt.event.ActionEvent evt) { String changeBarcode = barcodeTF.getText(); String changeName = nameTF.getText(); double changePrice = Double.parseDouble(costTF.getText()); try { DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Connection conn = DriverManager.getConnection("REMOVED"); ResultSet rs = conn.createStatement().executeQuery("UPDATE Test_items SET null," + changeBarcode + ", " + changeName + "," + changePrice + " WHERE "); } catch (Exception ex) { JOptionPane.showMessageDialog(rootPane, "Something is Wrong"); } } public void tableFiller (String searchType, String searchText) { try { DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Connection conn = DriverManager.getConnection("REMOVED"); //Access Rows and Columns from database Vector<String> Columns = new Vector<String>(); Vector<Vector<Object>> Rows = new Vector<Vector<Object>>(); ResultSet rs = conn.createStatement().executeQuery("select * from Test_items where " + searchType + " = " + searchText + ""); ResultSetMetaData metaDt = rs.getMetaData(); int cols = metaDt.getColumnCount(); Columns.clear(); // clear unwanted value if exist any in Columns variable.//Get Columns From database for (int i = 1; i <= cols; i++) { Columns.addElement(metaDt.getColumnName(i)); } Rows.clear(); // clear unwanted value if exist any in Rows variable. //Get RowsNames From database while (rs.next()) { Vector<Object> row = new Vector<Object>(cols); for (int i = 1; i <= cols; i++) { row.addElement(rs.getObject(i)); } Rows.addElement(row); } DefaultTableModel Model = new DefaultTableModel(Rows, Columns); searchResults.setModel(Model); searchResults.getColumnModel().getColumn(0).setPreferredWidth(50); searchResults.getColumnModel().getColumn(1).setPreferredWidth(100); searchResults.getColumnModel().getColumn(2).setPreferredWidth(200); searchResults.getColumnModel().getColumn(3).setPreferredWidth(100); listSelectionModel = searchResults.getSelectionModel(); listSelectionModel.addListSelectionListener(new SharedListSelectionHandler()); searchResults.getModel().addTableModelListener(searchResults); repaint(); conn.close(); } catch (Exception ex) { JOptionPane.showMessageDialog(rootPane, "Something is Wrong"); } } class SharedListSelectionHandler implements ListSelectionListener { @Override public void valueChanged(ListSelectionEvent e) { barcodeTF.setText(searchResults.getValueAt(searchResults.getSelectedRow(), 1).toString()); nameTF.setText(searchResults.getValueAt(searchResults.getSelectedRow(), 2).toString()); costTF.setText(searchResults.getValueAt(searchResults.getSelectedRow(), 3).toString()); } } public void tableChanged(TableModelEvent e) { searchResults.tableChanged(new TableModelEvent(searchResults.getModel())); } // Variables declaration - do not modify private javax.swing.JLabel barcodeLB; private javax.swing.JTextField barcodeTF; private javax.swing.JButton closeBT; private javax.swing.JButton comfirmBT; private javax.swing.JLabel costLB; private javax.swing.JTextField costTF; private javax.swing.JPanel jPanel1; private javax.swing.JLabel nameLB; private javax.swing.JTextField nameTF; private javax.swing.JButton searchByBT; private javax.swing.JComboBox searchByCB; private javax.swing.JLabel searchByLB; private javax.swing.JTextField searchByTF; private javax.swing.JTable searchResults; private javax.swing.JScrollPane searchResultsScrollpane; // End of variables declaration }
×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.