在我使用Java Swing的独立应用程序中,我使用ResultSet遍历Excel表(后端数据)。但在执行插入查询后,当我再次尝试使用ResultSet遍历记录时,它显示"ResultSet已关闭"。之后不能执行任何操作
错误是什么?我找不到。请告诉我。我是个新手。。所以请简单解释。我很感激你的帮助。谢谢:)
public sowui(String s)
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
con=DriverManager.getConnection("jdbc:odbc:test");
Statement stmt = con.createStatement();
stmt = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE );
String sql ="SELECT * FROM [Sheet1$] ";
rs =stmt.executeQuery(sql);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setTitle("MANAGEMENT OF STATEMENT OF WORKS");
setLayout(null);
l1=new JLabel("CONTRACT REFERENCE NUMBER");
t1=new JTextField(20);
l1.setBounds(50,100,250,50);
t1.setBounds(300,110,135,30);
t1.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input=t1.getText();
Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
t1.setText(null);
JOptionPane.showMessageDialog(null, "Please enter only numbers");
}
} });
l1m=new JLabel("*");
l1m.setBounds(260,100,10,50);
l1m.setForeground(Color.RED);
l2=new JLabel("CR NUMBER");
final String [] crn = {"N/A"};
c6 =new JComboBox(crn);
c6.setEditable(true);
c6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("comboBoxEdited")) {
System.out.println("Adding new player!");
c6.insertItemAt(c6.getSelectedItem(),0);
}
}
});
l2.setBounds(50,150,250,50);
c6.setBounds(300,160,135,30);
l2m=new JLabel("*");
l2m.setBounds(130,150,10,50);
l2m.setForeground(Color.RED);
l3=new JLabel("SOW NAME");
t3=new JTextField(20);
t3.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e) {
String input=t3.getText();
Pattern p = Pattern.compile("[0-9]");
Matcher m = p.matcher(input);
if (m.find())
{
t3.setText(null);
JOptionPane.showMessageDialog(null,"Enter only characters A-Z or a-z");
}
}
});
l3.setBounds(50,200,250,50);
t3.setBounds(300,210,135,30);
l3m=new JLabel("*");
l3m.setBounds(120,200,10,50);
l3m.setForeground(Color.RED);
l4=new JLabel("PARTNER SERVICE MANAGER");
t4=new JTextField(20);
t4.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e) {
String input=t4.getText();
Pattern p = Pattern.compile("[0-9]");
Matcher m = p.matcher(input);
if (m.find())
{
t4.setText(null);
JOptionPane.showMessageDialog(null,"Enter only characters A-Z or a-z");
}
}
});
l4.setBounds(50,250,250,50);
t4.setBounds(300,260,135,30);
l4m=new JLabel("*");
l4m.setBounds(230,250,10,50);
l4m.setForeground(Color.RED);
l5=new JLabel("LBG SERVICE MANAGER");
t5=new JTextField(20);
t5.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e) {
String input=t5.getText();
Pattern p = Pattern.compile("[0-9]");
Matcher m = p.matcher(input);
if (m.find())
{
t5.setText(null);
JOptionPane.showMessageDialog(null,"Enter only characters A-Z or a-z");
}
}
});
l5.setBounds(50,300,250,50);
t5.setBounds(300,310,135,30);
l5m=new JLabel("*");
l5m.setBounds(200,300,10,50);
l5m.setForeground(Color.RED);
l6=new JLabel("TYPE OF CONTRACT");
final String [] tct = {"SOW"};
c4 =new JComboBox(tct);
c4.setEditable(true);
c4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("comboBoxEdited")) {
System.out.println("Adding new player!");
c4.insertItemAt(c4.getSelectedItem(),0);
}
}
});
l6.setBounds(50,350,250,50);
c4.setBounds(300,360,135,30);
l6m=new JLabel("*");
l6m.setBounds(180,350,10,50);
l6m.setForeground(Color.RED);
l7=new JLabel("VALUE OF CONTRACT");
t7=new JTextField(20);
t7.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input=t7.getText();
Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
t7.setText(null);
JOptionPane.showMessageDialog(null, "Please enter only numbers");
}
} });
l7.setBounds(50,400,250,50);
t7.setBounds(300,410,135,30);
l7m=new JLabel("*");
l7m.setBounds(190,400,10,50);
l7m.setForeground(Color.RED);
l8=new JLabel("REMARKS");
t8=new TextArea(15,40);
l8.setBounds(50,450,250,50);
t8.setBounds(300,460,200,80);
l9=new JLabel("WORK STATUS");
final String [] workstatus = {"Live","Pipeline","Working at Risk"};
c1 =new JComboBox(workstatus);
c1.setEditable(true);
c1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("comboBoxEdited")) {
System.out.println("Adding new player!");
c1.insertItemAt(c1.getSelectedItem(),0);
}
}
});
l9.setBounds(500,100,250,50);
c1.setBounds(750,110,135,30);
l9m=new JLabel("*");
l9m.setBounds(600,100,10,50);
l9m.setForeground(Color.RED);
l10=new JLabel("CONTRACT APPROVAL");
final String [] cal = {"Fully signed off","WithPlatform","with partner for signoff"};
c2 =new JComboBox(cal);
c2.setEditable(true);
c2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("comboBoxEdited")) {
System.out.println("Adding new player!");
c2.insertItemAt(c2.getSelectedItem(),0);
}
}
});
l10.setBounds(500,150,250,50);
c2.setBounds(750,160,135,30);
l10m=new JLabel("*");
l10m.setBounds(640,150,10,50);
l10m.setForeground(Color.RED);
l11=new JLabel("PLATFORM");
final String [] PF = {"ADM-WPM","ADM-ALIS","ADM-SWeC","ADM-PAS","ADM-BSM","ADM-RAS","ADM-LP&I"};
c5 =new JComboBox(PF);
c5.setEditable(true);
c5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("comboBoxEdited")) {
System.out.println("Adding new player!");
c5.insertItemAt(c5.getSelectedItem(),0);
}
}
});
l11.setBounds(500,200,250,50);
c5.setBounds(750,210,135,30);
l11m=new JLabel("*");
l11m.setBounds(570,200,10,50);
l11m.setForeground(Color.RED);
l12=new JLabel("COMMERCIAL MODEL");
final String [] cml = {"T&M","FP","PSA"};
c3 =new JComboBox(cml);
c3.setEditable(true);
c3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("comboBoxEdited")) {
System.out.println("Adding new player!");
c3.insertItemAt(c3.getSelectedItem(),0);
}
}
});
l12.setBounds(500,250,250,50);
c3.setBounds(750,260,135,30);
l12m=new JLabel("*");
l12m.setBounds(630,250,10,50);
l12m.setForeground(Color.RED);
l13=new JLabel("START DATE");
t13=new JTextField(20);
l13.setBounds(500,300,250,50);
t13.setBounds(750,310,135,30);
t13.setEditable(false);
l13m=new JLabel("*");
l13m.setBounds(580,300,10,50);
l13m.setForeground(Color.RED);
l14=new JLabel("END DATE");
t14=new JTextField(20);
l14.setBounds(500,350,250,50);
t14.setBounds(750,360,135,30);
t14.setEditable(false);
l14m=new JLabel("*");
l14m.setBounds(570,350,10,50);
l14m.setForeground(Color.RED);
l15=new JLabel("CUMULATIVE VALUE OF CONTRACT");
t15=new JTextField(20);
t15.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input=t15.getText();
Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
t15.setText(null);
JOptionPane.showMessageDialog(null, "Please enter only numbers");
}
}});
l15.setBounds(500,400,250,50);
t15.setBounds(750,410,135,30);
l15m=new JLabel("*");
l15m.setBounds(710,400,10,50);
l15m.setForeground(Color.RED);
l16=new JLabel("SOW TRACKER");
l16.setBounds(400,50,250,50);
l16.setFont(new Font("TIMES NEW ROMAN", Font.BOLD, 28));
firstb=new JButton("FIRST");
firstb.setBounds(200,600,100,40);
prev=new JButton("PREVIOUS");
prev.setBounds(400,600,100,40);
nextb=new JButton("NEXT");
nextb.setBounds(600,600,100,40);
lastb=new JButton("LAST");
lastb.setBounds(800,600,100,40);
newb=new JButton("NEW");
newb.setBounds(200,650,100,40);
save=new JButton("SAVE");
save.setBounds(400,650,100,40);
exitb = new JButton("EXIT");
exitb.setBounds(600, 650,100,40);
searchb=new JButton("SEARCH");
searchb.setBounds(800,650,100,40);
b9=new JButton("..");
b9.setBounds(900,315,20,20);
b10=new JButton("..");
b10.setBounds(900,365,20,20);
add(l1);
add(t1);
add(l1m);
add(l2);
add(c6);
add(l2m);
add(l3);
add(t3);
add(l3m);
add(l4);
add(t4);
add(l4m);
add(l5);
add(t5);
add(l5m);
add(l6);
add(c4);
add(l6m);
add(l7);
add(t7);
add(l7m);
add(l8);
add(t8);
add(l9);
add(c1);
add(l9m);
add(l10);
add(c2);
add(l10m);
add(l11);
add(c5);
add(l11m);
add(l12);
add(c3);
add(l12m);
add(l13);
add(t13);
add(l13m);
add(l14);
add(t14);
add(l14m);
add(l15);
add(t15);
add(l15m);
add(l16);
add(firstb);
firstb.addActionListener(this);
add(prev);
prev.addActionListener(this);
add(nextb);
nextb.addActionListener(this);
add(lastb);
lastb.addActionListener(this);
add(newb);
newb.addActionListener(this);
add(save);
save.addActionListener(this);
add(exitb);
exitb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
add(searchb);
searchb.addActionListener(this);
add(b9);
b9.addActionListener(this);
add(b10);
b10.addActionListener(this);
setSize(1000,1000);
setVisible(true);
setResizable(false);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==save)
{
if(t1.getText().length()==0) { try{
JOptionPane.showMessageDialog(null, "Enter CONTRACT REFERENCE NUMBER"); }catch(Exception e){
JOptionPane.showMessageDialog(null, "JTextField is " + t1.getText()); }}
/*else if(t2.getText().length()==0) { try{
JOptionPane.showMessageDialog(null, "Enter CR NUMBER"); }catch(Exception e){
JOptionPane.showMessageDialog(null, "JTextField is " + t2.getText()); }}*/
else if(t3.getText().length()==0) { try{
JOptionPane.showMessageDialog(null, "enter text in JTextField-SOW NAME"); }catch(Exception e){
JOptionPane.showMessageDialog(null, "JTextField is " + t3.getText()); }}
else if(t4.getText().length()==0) { try{
JOptionPane.showMessageDialog(null, "enter text in JTextField-PARTNER SERVICE MANAGER"); }catch(Exception e){
JOptionPane.showMessageDialog(null, "JTextField is " + t4.getText()); }}
else if(t5.getText().length()==0) { try{
JOptionPane.showMessageDialog(null, "enter text in JTextField-LBG SERVICE MANAGER"); }catch(Exception e){
JOptionPane.showMessageDialog(null, "JTextField is " + t5.getText()); }}
else if(t7.getText().length()==0) { try{
JOptionPane.showMessageDialog(null, "Enter VALUE OF CONTRACT"); }catch(Exception e){
JOptionPane.showMessageDialog(null, "JTextField is " + t7.getText()); }}
/*else if(t11.getText().length()==0) { try{
JOptionPane.showMessageDialog(null, "enter text in JTextField-PLATFORM"); }catch(Exception e){
JOptionPane.showMessageDialog(null, "JTextField is " + t11.getText()); }}
else if(t12.getText().length()==0) { try{
JOptionPane.showMessageDialog(null, "enter text in JTextField-COMMERCIAL MODEL"); }catch(Exception e){
JOptionPane.showMessageDialog(null, "JTextField is " + t12.getText()); }}*/
else if(t13.getText().length()==0) { try{
JOptionPane.showMessageDialog(null, "Enter START DATE"); }catch(Exception e){
JOptionPane.showMessageDialog(null, "JTextField is " + t13.getText()); }}
else if(t14.getText().length()==0) { try{
JOptionPane.showMessageDialog(null, "Enter END DATE"); }catch(Exception e){
JOptionPane.showMessageDialog(null, "JTextField is " + t14.getText()); }}
else if(t15.getText().length()==0) { try{
JOptionPane.showMessageDialog(null, "Enter-CUMULATIVE VALUE OF CONTRACT"); }catch(Exception e){
JOptionPane.showMessageDialog(null, "JTextField is " + t15.getText()); }}
String str1=t13.getText();
String str2=t14.getText();
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Date date1 = null;
try {
date1 = (Date) df.parse(str1);
} catch (ParseException e) {
}
Date date2 = null;
try {
date2 = (Date) df.parse(str2);
} catch (ParseException e) {
}
if (date1.after(date2)) try{
JOptionPane.showMessageDialog(null, "Start Date must be earlier than the end date");
}catch(Exception e){}
if(ae.getSource()==save)
{
try
{
String value1=t1.getText();
String value2=(String)c6.getSelectedItem();
String value3=t3.getText();
String value4=t4.getText();
String value5=t5.getText();
String value6 = (String) c4.getSelectedItem();
String value7=t7.getText();
String value8=t8.getText();
String value9 = (String) c1.getSelectedItem();
String value10 = (String) c2.getSelectedItem();
String value11 = (String) c5.getSelectedItem();
String value12 = (String) c3.getSelectedItem();
String value13=t13.getText();
String value14=t14.getText();
String value15=t15.getText();
Statement st=con.createStatement();
System.out.println(value1);
// String q = "insert into [sheet1$] (CRN,CR_NUM,SOW_N,PSM,LBG,T_CONTRACT,V_CONTRACT,REMARKS,STATUS,APPROVAL,PLATFORM,MODEL,START_D,END_D,CUM_VALUE) values ('12345','"+value2+"','"+value3+"','"+value4+"','"+value5+"','"+value6+"','"+value7+"','"+value8+"','"+value9+"','"+value10+"','"+value11+"','"+value12+"','"+value13+"','"+value14+"','"+value15+"')";
String q = "insert into [sheet1$] values ('"+value1+"','"+value2+"','"+value3+"','"+value4+"','"+value5+"','"+value6+"','"+value7+"','"+value8+"','"+value9+"','"+value10+"','"+value11+"','"+value12+"','"+value13+"','"+value14+"','"+value15+"')";
int ds = st.executeUpdate(q);
System.out.println("Inserted Sucessfully...!!!");
System.out.println(ds);
}catch(Exception e)
{
System.out.println(e.getMessage());
}
finally{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
else if(ae.getSource()==newb)
{
try{
t1.setText(null);
c6.addItem(null);
t3.setText(null);
t4.setText(null);
t5.setText(null);
c4.addItem(null);
t7.setText(null);
t8.setText(null);
c1.addItem(null);
c2.addItem(null);
c5.addItem(null);
c3.addItem(null);
t13.setText(null);
t14.setText(null);
t15.setText(null);
}catch(Exception e){}
}
else if(ae.getSource()==firstb)
{
try {
rs.first( );
int id_col = rs.getInt("CRN");
String id = Integer.toString(id_col);
//int id_col1 = rs.getInt("CR_NUM");
//String cr= Integer.toString(id_col1);
String cr =rs.getString("CR_NUM");
String sow = rs.getString("SOW_N");
String psm = rs.getString("PSM");
String lbg = rs.getString("LBG");
String toc = rs.getString("T_CONTRACT");
String rm = rs.getString("REMARKS");
String wks = rs.getString("WORK_ST");
String cap = rs.getString("APPROVAL");
String pfm = rs.getString("PLATFORM");
String cmm = rs.getString("MODEL");
String std = rs.getString("START_D");
String etd= rs.getString("END_D");
int id_col2 = rs.getInt("V_CONTRACT");
String voc= Integer.toString(id_col2);
int id_col3 = rs.getInt("CUM_VALUE");
String cvc = Integer.toString(id_col3);
t1.setText(id);
//c6.addItem(cr);
t3.setText(sow);
t4.setText(psm);
t5.setText(lbg);
//c4.addItem(toc);
t7.setText(voc);
t8.setText(rm);
c1.setSelectedItem(wks);
c2.setSelectedItem(cap);
c5.setSelectedItem(pfm);
c3.setSelectedItem(cmm);
c4.setSelectedItem(toc);
c6.setSelectedItem(cr);
//c1.addItem(wks);
//c2.addItem(cap);
//c5.addItem(pfm);
//c3.addItem(cmm);
t13.setText(std);
t14.setText(etd);
t15.setText(cvc);
System.out.println(wks);
}
catch (SQLException err) {
JOptionPane.showMessageDialog(sowui.this, err.getMessage());
}
try{
stmt.close();
}catch(SQLException a) {}
}
else if(ae.getSource()==nextb)
{
try {
if ( rs.next( ) ) {
int id_col = rs.getInt("CRN");
String id = Integer.toString(id_col);
//int id_col1 = rs.getInt("CR_NUM");
//String cr= Integer.toString(id_col1);
String cr =rs.getString("CR_NUM");
String sow = rs.getString("SOW_N");
String psm = rs.getString("PSM");
String lbg = rs.getString("LBG");
String toc = rs.getString("T_CONTRACT");
String rm = rs.getString("REMARKS");
String wks = rs.getString("WORK_ST");
String cap = rs.getString("APPROVAL");
String pfm = rs.getString("PLATFORM");
String cmm = rs.getString("MODEL");
String std = rs.getString("START_D");
String etd= rs.getString("END_D");
int id_col2 = rs.getInt("V_CONTRACT");
String voc= Integer.toString(id_col2);
int id_col3 = rs.getInt("CUM_VALUE");
String cvc = Integer.toString(id_col3);
t1.setText(id);
//c6.addItem(cr);
t3.setText(sow);
t4.setText(psm);
t5.setText(lbg);
//c4.addItem(toc);
t7.setText(voc);
t8.setText(rm);
//c1.addItem(wks);
//c2.addItem(cap);
//c5.addItem(pfm);
//c3.addItem(cmm);
c1.setSelectedItem(wks);
c2.setSelectedItem(cap);
c5.setSelectedItem(pfm);
c3.setSelectedItem(cmm);
c4.setSelectedItem(toc);
c6.setSelectedItem(cr);
t13.setText(std);
t14.setText(etd);
t15.setText(cvc);
}
else {
rs.previous( );
JOptionPane.showMessageDialog(sowui.this, "End of File");
}
}
catch (SQLException err) {
JOptionPane.showMessageDialog(sowui.this, err.getMessage());
}try{
stmt.close();
}catch(SQLException a) {}
}
else if(ae.getSource()==prev)
{
try {
if ( rs.previous( ) ) {
int id_col = rs.getInt("CRN");
String id = Integer.toString(id_col);
//int id_col1 = rs.getInt("CR_NUM");
//String cr= Integer.toString(id_col1);
String cr =rs.getString("CR_NUM");
String sow = rs.getString("SOW_N");
String psm = rs.getString("PSM");
String lbg = rs.getString("LBG");
String toc = rs.getString("T_CONTRACT");
String rm = rs.getString("REMARKS");
String wks = rs.getString("WORK_ST");
String cap = rs.getString("APPROVAL");
String pfm = rs.getString("PLATFORM");
String cmm = rs.getString("MODEL");
String std = rs.getString("START_D");
String etd= rs.getString("END_D");
int id_col2 = rs.getInt("V_CONTRACT");
String voc= Integer.toString(id_col2);
int id_col3 = rs.getInt("CUM_VALUE");
String cvc = Integer.toString(id_col3);
t1.setText(id);
//c6.addItem(cr);
t3.setText(sow);
t4.setText(psm);
t5.setText(lbg);
//c4.addItem(toc);
t7.setText(voc);
t8.setText(rm);
//c1.addItem(wks);
//c2.addItem(cap);
//c5.addItem(pfm);
//c3.addItem(cmm);
c1.setSelectedItem(wks);
c2.setSelectedItem(cap);
c5.setSelectedItem(pfm);
c3.setSelectedItem(cmm);
c4.setSelectedItem(toc);
c6.setSelectedItem(cr);
t13.setText(std);
t14.setText(etd);
t15.setText(cvc);
}
else {
rs.next( );
JOptionPane.showMessageDialog(sowui.this, "Top of File");
}
}
catch (SQLException err) {
JOptionPane.showMessageDialog(sowui.this, err.getMessage());
}try{
stmt.close();
}catch(SQLException a) {}
}
if(ae.getSource()==lastb)
{
try {
rs.last( );
int id_col = rs.getInt("CRN");
String id = Integer.toString(id_col);
//int id_col1 = rs.getInt("CR_NUM");
//String cr= Integer.toString(id_col1);
String cr =rs.getString("CR_NUM");
String sow = rs.getString("SOW_N");
String psm = rs.getString("PSM");
String lbg = rs.getString("LBG");
String toc = rs.getString("T_CONTRACT");
String rm = rs.getString("REMARKS");
String wks = rs.getString("WORK_ST");
String cap = rs.getString("APPROVAL");
String pfm = rs.getString("PLATFORM");
String cmm = rs.getString("MODEL");
String std = rs.getString("START_D");
String etd= rs.getString("END_D");
int id_col2 = rs.getInt("V_CONTRACT");
String voc= Integer.toString(id_col2);
int id_col3 = rs.getInt("CUM_VALUE");
String cvc = Integer.toString(id_col3);
t1.setText(id);
//c6.addItem(cr);
t3.setText(sow);
t4.setText(psm);
t5.setText(lbg);
//c4.addItem(toc);
t7.setText(voc);
t8.setText(rm);
//c1.addItem(wks);
//c2.addItem(cap);
//c5.addItem(pfm);
//c3.addItem(cmm);
c1.setSelectedItem(wks);
c2.setSelectedItem(cap);
c5.setSelectedItem(pfm);
c3.setSelectedItem(cmm);
c4.setSelectedItem(toc);
c6.setSelectedItem(cr);
t13.setText(std);
t14.setText(etd);
t15.setText(cvc);
}
catch (SQLException err) {
JOptionPane.showMessageDialog(sowui.this, err.getMessage());
}try{
stmt.close();
}catch(SQLException a) {}
}
在注释中已经设置的内容旁边,还有一些关于您发布的代码的其他注释:
- 去掉
setLayout( null )
调用和所有相关调用(setBounds
等)。学会使用布局管理器 - Swing组件应该在事件调度线程上创建/访问/修改。该线程负责UI,不应用于长时间运行的任务(如处理
ResultSet
实例)。有关更多信息,请参阅Swing中的并发教程 JTextComponent
(在您的代码中,变量被称为t1
和t3
)的输入验证不应使用KeyListener
。请改用DocumentFilter
,或者如果是文本字段,请考虑使用JFormattedTextField
。例如,在文本字段中拖放输入将完全绕过KeyListener
- 请注意,侦听器中接收到的大多数事件都有一个
getSource()
方法,该方法允许检索事件发生的源组件。这可以避免大量的代码重复,因为您有多个侦听器,它们做完全相同的事情。它们唯一不同的部分是源组件,它可以从事件中检索。另一种选择是从匿名类切换到内部类,并在构造函数中传递所需的信息 - 简单地用
try{} catch ( Exception e )
包围随机的代码片段,然后不做任何错误处理,这就是在未来会出现奇怪的错误。要么你正确地处理了Exception
,要么你把它扔得更远 - 空的
catch
块甚至更糟。现在,您可以默默地忽略错误,代码会像什么都没发生一样继续执行。谁知道你的程序将以何种状态结束
Resultset绑定到您的语句和连接。您应该在打开数据库连接时读取ResultSet的内容,然后关闭ResultSet、语句,然后关闭连接。
请注意,您不必将结果读取到UI中——您也可以将它们读取到一些内部集合中,然后在需要时更新UI。只要把它们从结果集中取出来,同时仍然有对提到的对象的引用。