Updating Database by editing jtable

I am new to java and i have a problem which i want to resolve. I recently saw a code which basically lists the values of a database in a jtable, so i modified it and used it in my own app. The problem which i have now come across is the updating of the database by editing the jtable. I have no idea how to do this, the only thing i know is that i have to call an event like firecellupdated but i dont know how to call it and what to call in it and also where to call it.

Can anyone help me on this issue. Any help would be appreciated.


Note: Here is the listing code in case you require it

// Connect to an MySql Database String driver = "com.mysql.jdbc.Driver"; // String url = "jdbc:odbc: "; // if using ODBC Data Source name String url = "jdbc:mysql://localhost:3306/tpos"; String userid = "root"; String password = "root"; Class.forName( driver ); Connection connection = DriverManager.getConnection( url, userid, password ); // Read data from a table pstat = connection.prepareStatement("Select * from supplier where SupplierId="); pstat.setString(1,search); //String sql = "Select * from supplier where SupplierId="; //Statement stmt = connection.createStatement(); ResultSet rs = pstat.executeQuery(); ResultSetMetaData md = rs.getMetaData(); int columns = md.getColumnCount(); // Get column names for (int i = 1; i

Posted On: Wednesday 17th of October 2012 05:32:15 AM Total Views:  374
View Complete with Replies

Related Messages:

Updating edited tree to database   (134 Views)
I have jtree in a frame which can be editable. I have written the data present in it into database. Now, i made some changes in the jtree and i want to make them updated automatically to database when i edit tree. How to do it Please suggest the basic structure of the program
Update MySQL table by updating JTable's cell   (316 Views)
I have a JTable which is used to show and modify table from a MySQL database. I have no problem to retrieve/show data in JTable or insert new data into a blank table. However, I have problem to update any particular cell in the JTable and reflect the changes in the MySQL database. If there are hundreds of rows and I update couple cells within around 10 rows, and I create a save button to apply all the changes to MySQL's table. what's the proper way to do that It will include so many UPDATE statement for every row I changed, how do I detect any change I made in cells efficiently and save all the change back to the DB at once.
Problems updating JTextField in a new thread   (159 Views)
I know others have posted about this, but I am still not understanding how this is supposed to work. My thought was to have a LogHandler class to which I would pass a JTextField. Then anywhere I felt the need to pass the status of my operations, I could just update the text. The problem is that I don't see any of the intermediate messages. I have been trying to get JTextField updating in a separate thread, but with no luck. The println shows messages scrolling in the text window, but I don't see them in the text field, just the last one, so I assume they are still all updating at once when the event queue empties. I left my various failed attempts commented in the code. Could someone help me understand what I am doing wrong import*; import java.nio.*; import java.util.*; import java.awt.*; import javax.swing.*; // // Set up a place to for all methods to call for logging results either to // the field in the main dialog or to a file. public final class LogHandler { // // Only one location for the log messages to go. // private static JTextField logField = null; // private static String logString = null; private static UpdateText runUpdate; private static Thread updateThread; public static boolean setLogField( JTextField tf ) { if (logField == null) { System.out.println("Assigning Log Field..."); logField = tf; runUpdate = new UpdateText( logField ); // updateThread = new Thread( runUpdate ); // updateThread.start(); } return true; } public static void writeLogField( final String logString ) { if (logField != null) { System.out.println("Writing: " + logString); // logField.setText( logString ); runUpdate.setText( logString ); // updateThread = new Thread( runUpdate ); // updateThread.start(); // SwingUtilities.invokeLater( runUpdate ); SwingUtilities.invokeLater( new Runnable() { public void run() { logField.setText( logString ); } }); } } public static void clearLogField() { if (logField != null) { logField.setText(""); } } static class UpdateText implements Runnable { JTextField field; String text; public UpdateText( JTextField field ) { this.field = field; } public void setText( String text ) { this.text = text; } @Override public void run() { field.setText( text ); } } }
Update database by editing the cells in JTable   (208 Views)
i have created a form in which i added a jtable which i guess is of type defaulttable model DefaultTableModel model=(DefaultTableModel)jTable1.getModel(); model.setRowCount(0);
preserve line breaks from jtextarea when storing in database   (178 Views)
how to preserve line breaks of jtextarea while storing and retrieving text from database(oracle)
jcombobox is not auto updating with database.   (233 Views)
hello all, i have a problem, actually i have posted this problem earlier, and michael dunn helps me solve that problem. Below is the problem. but now i m getting the same problem in auto updating jcomboboxes. i have two code one is updating jcombobox with arraylist, and working properly. one is with db that is not working. code for both are same, instead of arraylist i am just using database. i also have done debugging by putting bunch system.out.println to check the flow, both giving the same sequence of statements, i mean flow of both are same. i don't know what wrong is going there, can anyone please help me in that. below is the code with db which is not working. import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; import java.sql.*; class Display { CardLayout cl = new CardLayout(); JPanel cardPanel = new JPanel(cl); JPanel panel = new JPanel(); JTextField tf = new JTextField(15); ArrayList resultSetList = new ArrayList(); JButton btn = new JButton("Add Student"); JFrame frame = new JFrame(); JComboBox studentNames = new JComboBox(); DefaultComboBoxModel dcm = new DefaultComboBoxModel(); LoadStudents ls = new LoadStudents(); SaveStudent ss = new SaveStudent(); Display1 d1 = new Display1(); public Display() throws SQLException { System.out.println("Inside WithArrayList Constructor"); cardPanel.add(panel,"display"); cardPanel.add(d1.panel1,"display1"); panel.add(tf); panel.add(btn); frame.add(cardPanel); frame.setVisible(true); frame.setSize(200,100); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try{ System.out.println("Inside WithArrayList ActionListener"); String values = tf.getText(); ss.saveStudent(values); System.out.println("Inside WithArrayList ActionListener after savestudent"); tf.setText(""); System.out.println("Inside WithArrayList ActionListener Before d1.update"); d1.update(); System.out.println("Inside WithArrayList ActionListener After d1.update");,"display1"); }catch(Exception e){} } }); } public static void main(String args[]) throws SQLException{new Display();} class Display1 { JPanel panel1 = new JPanel(); JComboBox studentNames = new JComboBox(); DefaultComboBoxModel dcm = new DefaultComboBoxModel(); LoadStudents ls = new LoadStudents(); SaveStudent ss = new SaveStudent(); public Display1() throws SQLException { System.out.println("Inside Constructor Display1"); ls.loadStudents1(studentNames); System.out.println("Inside Constructor Display1 After Calling ls.loadStudents1"); panel1.add(studentNames); } public void update() throws SQLException { System.out.println("Inside Update method of Display1 class"); ls.loadStudents1(studentNames); System.out.println("Inside Update method of Display1 class After Calling loadstudents1"); } } class LoadStudents { DefaultComboBoxModel model; public void loadStudents1(JComboBox cbo) throws SQLException { System.out.println("Inside loadStudents1 of class LoadStudents Beginning"); ArrayList ress; Db db = new Db(); model = new DefaultComboBoxModel(); ress = db.NameValues(); ListIterator iter = ress.listIterator(); while (iter.hasNext()) { model.addElement(; } cbo.setModel(model); System.out.println("Inside loadStudents1 of class LoadStudents End"); } } class SaveStudent { public void saveStudent(String values) throws SQLException { System.out.println("Inside saveStudent of class saveStudent Beginning"); Db db = new Db(); db.Insert1(values); System.out.println("Inside saveStudent of class saveStudent End"); } } class Db { public ArrayList NameValues()throws SQLException{ System.out.println("Inside NameValues of class Db Beginning"); Statement ps; ResultSet res; String query = "SELECT names FROM studentregistration"; DbConnection dbconn = new DbConnection(); Connection conn = dbconn.dbConn(); ps = conn.createStatement(); res = ps.executeQuery(query); while({ resultSetList.add(res.getString("names")); } return resultSetList; } public void Insert1(String nm) { try{ PreparedStatement st; String query = "INSERT into studentregistration (names) VALUES ()"; DbConnection dbconn = new DbConnection(); Connection conn = dbconn.dbConn(); st = conn.prepareStatement(query); st.setString(1,nm); int rr = st.executeUpdate(); System.out.println("Inside Insert1 of class Db Beginning"); }catch(Exception e){e.printStackTrace();} } } }
Jtextfield is not updating dynamically(at run time)   (167 Views)
Hi all, I am trying to update jtextfield at runtime with new values, which are fetched from a hashmap. But the jtextfield text is not updating. Please refer to the below code. Please suggest, where i am going wrong.
Connect java with database through java.swing*   (166 Views)
to , I'm beginner in Java programming.. I have an exam with programming, i have a task to create a application with java.swing*, through swing* , I have to create a connection with database oracle. I need a code with swing, a text field, when i type a database query, this query will execute and will get the data in database oracle and will show them in application. I hope this will be understating for you.
How to add a columns with a button set to a Jtable built with database result set   (201 Views)
hi, i have to build a gui to display account numbers and account balances of a particular person, after he logged with the correct user name and password. and when he get the account number and balances in a table, there should be another button to get further details of a account such as transactions, amount of each transaction etc. now i have stucked in the point of adding that button column to the table which is built with database result set. i would
Get ID value on database from JCombo   (160 Views)
!!! I have a JCombo with names, each name has a ID in my database, I need to get that ID each time I pick a item from my JCombo. I try something like this: CPerson person = (CPerson) JCombo.getSelectedItem(); String id = person.getID();
problem in fetching values in combobox from databases.   (202 Views)
hello, i m storing date in string format in database, actually i have created three JComboBoxes for Date, month, year. and i m just appending all the values of all comboboxes before inserting into databases and then inserting into database. now i need to fetch values from database and display in their combo boxes (i mean: date should be in date combo, month should be in month, so on..) how can i do this although i have not added any delimiter to string, so how do i split the string so i got respective values in respective comboboxes
JList model elements pre exist, updating of vector does not change model   (168 Views)
so a few entries already exist in the jList. however, once the vector "caller" gets updated the values are not updated in the jList. Please help! thanks! public void split1(Vector v1) { this.caller = v1; left = new JPanel(); left.setLayout(new BorderLayout()); DefaultListModel model = new DefaultListModel(); customerList = new JList(model); scroll = new JScrollPane(customerList); scroll.setPreferredSize(new Dimension(150, 500)); left.add(scroll); String[] entries = {"one", "two", "three"}; for (int i = 0; i < entries.length; i++) { model.addElement(entries[i]); } if (caller != null) { String hell = caller.get(0) + " " + caller.get(1); validate(); int index = customerList.getSelectedIndex(); // get selected index if (index == -1) { // no selection, so insert at beginning index = 0; } else { // add after the selected item index++; } model.addElement(hell); customerList.setSelectedIndex(index); customerList.ensureIndexIsVisible(index); customerList.repaint(); scroll.revalidate(); scroll.repaint(); } }
Convert the .Tiff file to .png and insert into the database.   (235 Views)
I am new to java, I am trying to convert the .tiff file to .png and then insert it into the oracle database. can anyhow help me how to do as I have done some coding to pick the file and dispaly it into the item field but I don't how to convet the insert it into the database. Any help will be higly appreciated. import java.lang.Class; import java.sql.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import; import; import javax.swing.*; import org.im4java.core.ConvertCmd; import org.im4java.core.IMOperation; import java.util.*; import org.im4java.core.IM4JavaException; //Main class public class gui1{ //Declare variables static JFrame frame1; static Container pane; static JButton btnConnect, btnBrowse, btnUpload; static JLabel lblfileno, lblFilename, lblTitle; static JTextField txtfileno, txtFilename; static Insets insets; public static void main (String args[]){ //Set Look and Feel try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (ClassNotFoundException e) {} catch (InstantiationException e) {} catch (IllegalAccessException e) {} catch (UnsupportedLookAndFeelException e) {} //Create the frame frame1 = new JFrame ("Sample GUI Application"); frame1.setSize (600,300); pane = frame1.getContentPane(); insets = pane.getInsets(); pane.setLayout (null); String filename = File.separator+"tmp"; JFileChooser fc = new JFileChooser(new File(filename)); //Create controls btnConnect = new JButton ("Convert File"); btnBrowse = new JButton ("Browse"); btnUpload = new JButton ("Upload"); lblfileno = new JLabel ("File #:"); lblFilename = new JLabel ("File Name:"); lblTitle = new JLabel ("Sample Demo"); txtfileno = new JTextField (30); txtFilename = new JTextField (50); //Add all components to panel pane.add (lblfileno); pane.add (lblFilename); pane.add (lblTitle); pane.add (txtfileno); pane.add (txtFilename); pane.add (btnConnect); pane.add (btnBrowse); pane.add (btnUpload); //Place all components lblTitle.setBounds (insets.left + 250, + 35, lblTitle.getPreferredSize().width +10, lblTitle.getPreferredSize().height +10); lblFilename.setBounds (insets.left + 27, + 75, lblFilename.getPreferredSize().width, lblFilename.getPreferredSize().height); txtFilename.setBounds (lblFilename.getX() + lblFilename.getWidth() + 5, + 75, txtFilename.getPreferredSize().width, txtFilename.getPreferredSize().height); btnBrowse.setBounds (txtFilename.getX() + txtFilename.getWidth() + 5, + 75, btnBrowse.getPreferredSize().width, btnBrowse.getPreferredSize().height); lblfileno.setBounds (insets.left + 27, + 100, lblfileno.getPreferredSize().width, lblfileno.getPreferredSize().height); txtfileno.setBounds (lblfileno.getX() + lblfileno.getWidth() + 25, + 100, txtfileno.getPreferredSize().width, txtfileno.getPreferredSize().height); btnConnect.setBounds (insets.left + 150, + 155, btnConnect.getPreferredSize().width, btnConnect.getPreferredSize().height); btnUpload.setBounds (insets.left + 250, + 155, btnUpload.getPreferredSize().width, btnUpload.getPreferredSize().height); frame1.setVisible (true); btnBrowse.addActionListener(new OpenFileAction(frame1, fc)); } public static class btnConnectAction implements ActionListener{ public void actionPerformed (ActionEvent e){ System.out.println("You entered "+txtFilename.getText()); } } public static class OpenFileAction extends AbstractAction { JFrame frame; JFileChooser chooser; OpenFileAction(JFrame frame, JFileChooser chooser) { super("Open..."); this.chooser = chooser; this.frame = frame; } public void actionPerformed(ActionEvent evt) { // Show dialog; this method does not return until dialog is closed chooser.showOpenDialog(frame); // Get the selected file File file = chooser.getSelectedFile(); txtFilename.setText(file.getAbsolutePath());//+"/"+file.getName()); } }; // This action creates and shows a modal save-file dialog. public class SaveFileAction extends AbstractAction { JFileChooser chooser; JFrame frame; SaveFileAction(JFrame frame, JFileChooser chooser) { super("Save As..."); this.chooser = chooser; this.frame = frame; } public void actionPerformed(ActionEvent evt) { // Show dialog; this method does not return until dialog is closed chooser.showSaveDialog(frame); // Get the selected file File file = chooser.getSelectedFile(); } }; }
displaying data from database   (163 Views)
hi , i have created my first gui projects interface. the interface consists of a few combo boxes and buttons. now what i want is a simple tutorial that teaches me how to retrieve information form a back end database using the values input by the user into the interface. any suggestions and i was also wondering where i can learn how to display a new window on clicking a button and also how to display new contents in an already visible window i.e wiping off entire components being displayed on a window and replacing them with new components.
How to insert date values to database using java swings   (188 Views)
, can any buddy tell me how to insert date values from a calender to data base by using java swings
Inserting single column values of database table into multiple columns of jtable and do comparison   (336 Views)
all, I am struck ed in inserting single column values of database table into multiple columns of jtable and doing comparison for selecting greater values in each row. Is their any query that splits column values. here i am attaching the code please give me the solution. public void disp( final String n, final String p, final String f, final String t){ try{ rs1=st.executeQuery("SELECT factname,monval FROM tsi where frm='"+p+"'"); //IN('"+p+"',"+"'"+f+"',"+"'"+t+"')"); while( { a = rs1.getString("factname"); b= rs1.getString("monval"); //c= rs.getString("mon"); rows1++; }System.out.println(rows1); rs2=st.executeQuery("SELECT monval FROM tsi where frm='"+f+"'"); while( { b= rs2.getString("monval"); rows2++; }System.out.println(rows2);/*// data2=new Object[rows2][1];int i2=0; rs2=st.executeQuery("SELECT monval FROM tsi where frm='"+f+"'"); for(int i1=0;i1
Problem updating a JTable in a tab before the tab is opened   (77 Views)
I change the contents of a JTable when running code from a user's action in the currently opened tab (I am using Java 1.6). To update the contents I change the data in my TableModel class that extends DefaultTableModel so that the methods getRowCount, getColCount and getValueAt return the correct values. However, when I go to the tab that contains the table it is not displaying the updated contents. I have tried using invalidate and repaint to no avail; I don't know what happens when the RepaintManager queues the requests (I think it is used for managing components currently in view). However, if I close and restart the program the updated data (which comes from a database) is displayed correctly. I want to invoke repaint immediately after the table is displayed on opening the tab; the question is what kind of event can I use to do this