【发布时间】:2015-05-10 06:58:55
【问题描述】:
我创建了一个 java swing 应用程序,其中基本上用户登录到主窗口并选择一个 jmenu 项,该项目将用户引导到一个新窗口,您可以在其中向数据库输入数据。
在更新数据库的第三个窗口中,我使用了一个 jcombobox,其中的项目是从数据库中加载的。
当我调试它时,它运行正常。但是当我尝试从上到下运行应用程序时,会显示更新窗口,但未加载 jcombobox 项。它给出了一个错误,说连接太多。
据我所知,我已正确关闭所有连接。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String un=jTextField1.getText();
String pwd=jPasswordField1.getText();
if(un.isEmpty()){
JOptionPane.showMessageDialog(this,"User Name is empty");
}
else if(pwd.isEmpty()){
JOptionPane.showMessageDialog(this,"Password is empty");
}
else{
try {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
Calendar cal = Calendar.getInstance();
ResultSet rs=new DBconnect().getdata("SELECT * FROM user");
rs.next();
if ((rs.getString("Name").equals(un))&&(rs.getString("pw").equals(pwd))){
new DBconnect().putdata("INSERT INTO login (Date,User) VALUES('"+dateFormat.format(cal.getTime())+"','"+un+"')");
new MainWindow().setVisible(true);
this.dispose();
}
else{
JOptionPane.showMessageDialog(this, "Invalid user name or password");
jTextField1.setText("");
jPasswordField1.setText("");
}
rs.close();
} catch (Exception ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
以上是我的登录表单代码。
public MainWindow() {
setExtendedState(JFrame.MAXIMIZED_BOTH);
new Thread(){
public void run(){
while(true){
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
DateFormat dateFormat2 = new SimpleDateFormat("yyyy/MM/dd");
Calendar cal2 = Calendar.getInstance();
jLabel1.setText(dateFormat.format(cal.getTime()));
try {
ResultSet rs=new DBconnect().getdata("SELECT COUNT(Pno) FROM medicalhistory WHERE Date ='"+dateFormat2.format(cal2.getTime())+"'");
rs.next();
jLabel4.setText(rs.getString("COUNT(Pno)").toString());
ResultSet rs2=new DBconnect().getdata("SELECT SUM(Amount) FROM income WHERE Date ='"+dateFormat2.format(cal2.getTime())+"'");
rs2.next();
jLabel5.setText(rs2.getString("SUM(Amount)").toString());
rs2.close();
rs.close();
} catch (Exception ex) {
Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}.start();
initComponents();
}
上面是我使用连接的主窗口代码。这些工作正常。
public addnewpatient() {
setExtendedState(JFrame.MAXIMIZED_BOTH);
initComponents();
try {
try (ResultSet rs3 = new DBconnect().getdata("SELECT Name FROM drugstock WHERE stockAmount >0")) {
Vector v= new Vector();
while(rs3.next()){
String ids = rs3.getString("Name");
v.add(ids);
jComboBox1.addItem(ids);
}
jComboBox1.addItem("Null");
rs3.close();
}
}
catch (Exception ex) {
Logger.getLogger(addnewpatient.class.getName()).log(Level.SEVERE, null, ex);
}
}
上面的代码给出了太多的连接错误。
putdata 和 getdata 是我在连接类中创建的两个方法,以方便使用。
:) 提前感谢:)
【问题讨论】:
-
请附上您在这篇文章中看到的例外情况。
标签: java jdbc connection