Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieving text from JTextField
    text
    copied!<p>The concept of my code is that, it will initially retrieve distinct names from a column 'tname' of my access database table 'try'. It will add those items in a combobox. Once we select an item in the combo box, the data of the row containing tname as the selected item is retrieved and showed in textfields. Then I will make some changes to the text field content. After that, if I click 'Save' button, then all the data of the row containing tname as the selected combobox item must be updated with the new content in the textfields.</p> <p>Everything goes fine, except the last one. When I click 'save' it considers only the previous text(the one intially retrieved from the database when we select combobox) and not the changes made to it. Kindly help me to diagnose the problem.</p> <p>Thanks in advance.</p> <pre><code>import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.table.*; import java.util.Date; import java.sql.*; import java.text.*; public class gut implements ActionListener { JComboBox ctn; JTextField cm,exd,stk,cst,sup,snum,r; String stn,scm,sexd,sst,scst,ssup,ssnum,sr,icm,iexd,istk,icst,isup,isnum,ir; JLabel lt,lc,le,ls,lcs,lsp,lspn,lr; JButton s; JFrame gp=new JFrame(); public gut() { gp.setSize(500,500); gp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gp.setLayout(null); lt=new JLabel("Tablet Name",JLabel.RIGHT); lc=new JLabel("Composition",JLabel.RIGHT); le=new JLabel("Expiry Date (dd/mm/yyyy)",JLabel.RIGHT); ls=new JLabel("Stock",JLabel.RIGHT); lcs=new JLabel("Cost",JLabel.RIGHT); lsp=new JLabel("Supplier",JLabel.RIGHT); lspn=new JLabel("Supplier Number",JLabel.RIGHT); lr=new JLabel("Rack",JLabel.RIGHT); lt.setBounds(100,120,120,20); lc.setBounds(100,140,120,20); le.setBounds(60,160,160,20); ls.setBounds(100,180,120,20); lcs.setBounds(100,200,120,20); lsp.setBounds(100,220,120,20); lspn.setBounds(100,240,120,20); lr.setBounds(100,260,120,20); ctn=new JComboBox(); cm=new JTextField(); exd=new JTextField(); stk=new JTextField(); cst=new JTextField(); sup=new JTextField(); snum=new JTextField(); r=new JTextField(); ctn.setBounds(240,120,120,20); cm.setBounds(240,140,120,20); exd.setBounds(240,160,120,20); stk.setBounds(240,180,120,20); cst.setBounds(240,200,120,20); sup.setBounds(240,220,120,20); snum.setBounds(240,240,120,20); r.setBounds(240,260,120,20); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn=DriverManager.getConnection("jdbc:odbc:vasantham","",""); Statement st=conn.createStatement(); ResultSet rs=st.executeQuery("select DISTINCT tname from try"); while(rs.next()) { ctn.addItem(rs.getString("tname")); } conn.close(); } catch(Exception e) { } gp.add(lt);gp.add(ctn); gp.add(lc);gp.add(cm); gp.add(le);gp.add(exd); gp.add(ls);gp.add(stk); gp.add(lcs);gp.add(cst); gp.add(lsp);gp.add(sup); gp.add(lspn);gp.add(snum); gp.add(lr);gp.add(r); ctn.addActionListener(this); s=new JButton("Save"); s.setBounds(200,300,100,20); gp.add(s); s.addActionListener(this); gp.setVisible(true); } public void actionPerformed(ActionEvent evt) { String act=evt.getActionCommand(); String scb=(String)ctn.getSelectedItem(); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn=DriverManager.getConnection("jdbc:odbc:vasantham","",""); Statement st=conn.createStatement(); ResultSet rs=st.executeQuery("select * from try where tname='"+scb+"'"); SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy"); while(rs.next()) { icm=rs.getString("composition"); iexd=formatter.format(rs.getDate("exdate")); istk=Integer.toString(rs.getInt("stock")); icst=rs.getString("cost"); isup=rs.getString("sup"); isnum=rs.getString("supnum"); ir=rs.getString("rack"); } cm.setText(icm); exd.setText(iexd); stk.setText(istk); cst.setText(icst); sup.setText(isup); snum.setText(isnum); r.setText(ir); conn.close(); } catch(Exception e) { System.out.println(e); } if(act.equals("Save")) { scm=cm.getText(); sexd=exd.getText(); sst=stk.getText(); scst=cst.getText(); ssup=sup.getText(); ssnum=snum.getText(); sr=r.getText(); System.out.println(scm+","+sexd+","+sst+","+scst+","+ssup+","+ssnum+","+sr); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn=DriverManager.getConnection("jdbc:odbc:vasantham","",""); PreparedStatement ps=conn.prepareStatement("UPDATE try set composition=?,exdate=?,stock=?,cost=?,sup=?,supnum=?,rack=? where tname=?"); ps.setString(1,scm); ps.setString(2,sexd); ps.setString(3,sst); ps.setString(4,scst); ps.setString(5,ssup); ps.setString(6,ssnum); ps.setString(7,sr); ps.setString(8,scb); ps.executeUpdate(); JOptionPane.showMessageDialog(null,"Your entry has been stored successfully!!!"); } catch(Exception e) { JOptionPane.showMessageDialog(null,"Error!Try again!"); System.out.println(e); } } } public static void main(String[] args) { new gut(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload