我的代码的概念是,它最初会从我的访问数据库表“try”的列“tname”中检索不同的名称。它将在组合框中添加这些项目。一旦我们在组合框中选择了一个项目,包含 tname 作为所选项目的行的数据就会被检索并显示在文本字段中。然后我将对文本字段内容进行一些更改。之后,如果我单击“保存”按钮,则必须使用文本字段中的新内容更新包含 tname 作为所选组合框项的行的所有数据。
一切都很好,除了最后一个。当我单击“保存”时,它只考虑前一个文本(当我们选择组合框时从数据库中检索到的文本)而不是对其所做的更改。请帮助我诊断问题。
提前致谢。
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();
}
}