连接数据库的代码出现死循环,求大神相助。

时间:2022-09-13 21:44:12

import java.awt.BorderLayout;



public class Login extends JFrame {

private JPanel contentPane;
private JTextField textField;
private TextField passwordField;

/**
 * Launch the application.
 */
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
 * Create the frame.
 */
public Login() {
setTitle("\u8D44\u4EA7\u6E05\u67E5\u7CFB\u7EDF");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 455, 274);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);

JLabel label = new JLabel("\u6B22\u8FCE\u4F7F\u7528\u5357\u4EAC\u6653\u5E84\u5B66\u9662\u8D44\u4EA7\u6E05\u67E5\u7CFB\u7EDF");
label.setFont(new Font("微软雅黑", Font.BOLD, 13));
panel.add(label);

JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.CENTER);

JLabel label_1 = new JLabel("\u7528\u6237\u540D \uFF1A");
label_1.setFont(new Font("宋体", Font.BOLD, 12));

JLabel label_2 = new JLabel("\u5BC6  \u7801 \uFF1A");
label_2.setFont(new Font("宋体", Font.BOLD, 12));

textField = new JTextField();
textField.setColumns(10);

passwordField = new TextField();
passwordField.setEchoChar('*');
GroupLayout gl_panel_1 = new GroupLayout(panel_1);
gl_panel_1.setHorizontalGroup(
gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(94)
.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(label_2)
.addGap(18)
.addComponent(passwordField, 118, 118, 118))
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(label_1)
.addGap(18)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(145, Short.MAX_VALUE))
);
gl_panel_1.setVerticalGroup(
gl_panel_1.createParallelGroup(Alignment.TRAILING)
.addGroup(Alignment.LEADING, gl_panel_1.createSequentialGroup()
.addGap(41)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_1))
.addGap(38)
.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
.addComponent(label_2)
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addContainerGap(44, Short.MAX_VALUE))
);
panel_1.setLayout(gl_panel_1);

JPanel panel_2 = new JPanel();
contentPane.add(panel_2, BorderLayout.SOUTH);

JButton button = new JButton("\u767B  \u9646");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(textField.getText().equals("")){
new JOptionPane().showMessageDialog(null,"用户名不能为空!");
}
else if(passwordField.getText().equals("")){
new JOptionPane().showMessageDialog(null,"密码不能为空!");
}
else{
System.out.println("print_else");

try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("数据库驱动加载成功");
}catch(Exception e){
System.out.println("数据库驱动加载失败");
e.printStackTrace();
}

try{
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/AssetsManageSystem","root","lhakuma");
System.out.println("数据库服务器连接成功");
java.sql.Statement stmt = con.createStatement(); 
    ResultSet rs = stmt.executeQuery("select * from user"); 
while(rs.next()){
if(textField.getText().equals(rs.getString("name"))&&passwordField.getText().equals(rs.getString("pass"))){
new MainFrame();
break;
}

else{
new JOptionPane().showMessageDialog(null,"用户名或者密码错误!请重新输入");
}

}
}
catch(Exception e2){
System.out.println("获取数据失败");
e2.printStackTrace();
}

}

});
JButton button_1 = new JButton("\u6E05  \u7A7A");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
passwordField.setText("");
}
});

JButton button_2 = new JButton("\u9000  \u51FA");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
panel_2.add(button);
panel_2.add(button_1);
panel_2.add(button_2); 
}
}

以上是主窗体的代码,问题出在两个try的函数体里。
调用的第二个类:

import java.awt.BorderLayout;


public class MainFrame extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
 * Create the frame.
 */
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
main(null);
}

}

数据库是MySQL的,我建立的数据库是叫AssetsManageSystem,表名是user。

4 个解决方案

#1


数据库连接要释放的。。不然运行几次 就Over.

#2


try{                        Connection con = DriverManager.getConnection(                        "jdbc:mysql://localhost:3306/AssetsManageSystem","root","lhakuma");                        System.out.println("数据库服务器连接成功");                        java.sql.Statement stmt = con.createStatement();                         ResultSet rs = stmt.executeQuery("select * from user");                         while(rs.next()){                            if(textField.getText().equals(rs.getString("name"))&&passwordField.getText().equals(rs.getString("pass"))){                                new MainFrame();                                break;                            }                                                         else{                                new JOptionPane().showMessageDialog(null,"用户名或者密码错误!请重新输入");                            }                                                     }                    }                    catch(Exception e2){                        System.out.println("获取数据失败");                        e2.printStackTrace();                    }
finally
{
 // 结果集 连接释放 
rs,close();
con。close();
}

#3


引用 2 楼 xuemingyuan88 的回复:
try{                        Connection con = DriverManager.getConnection(                        "jdbc:mysql://localhost:3306/AssetsManageSystem","root","lhakuma");                        System.out.println("数据库服务器连接成功");                        java.sql.Statement stmt = con.createStatement();                         ResultSet rs = stmt.executeQuery("select * from user");                         while(rs.next()){                            if(textField.getText().equals(rs.getString("name"))&&passwordField.getText().equals(rs.getString("pass"))){                                new MainFrame();                                break;                            }                                                         else{                                new JOptionPane().showMessageDialog(null,"用户名或者密码错误!请重新输入");                            }                                                     }                    }                    catch(Exception e2){                        System.out.println("获取数据失败");                        e2.printStackTrace();                    }
finally
{
 // 结果集 连接释放 
rs,close();
con。close();
}


我加了那两句话,可是。。。。 连接数据库的代码出现死循环,求大神相助。出现这个错误时怎么回事??

#4


rs定义在try块外面

#1


数据库连接要释放的。。不然运行几次 就Over.

#2


try{                        Connection con = DriverManager.getConnection(                        "jdbc:mysql://localhost:3306/AssetsManageSystem","root","lhakuma");                        System.out.println("数据库服务器连接成功");                        java.sql.Statement stmt = con.createStatement();                         ResultSet rs = stmt.executeQuery("select * from user");                         while(rs.next()){                            if(textField.getText().equals(rs.getString("name"))&&passwordField.getText().equals(rs.getString("pass"))){                                new MainFrame();                                break;                            }                                                         else{                                new JOptionPane().showMessageDialog(null,"用户名或者密码错误!请重新输入");                            }                                                     }                    }                    catch(Exception e2){                        System.out.println("获取数据失败");                        e2.printStackTrace();                    }
finally
{
 // 结果集 连接释放 
rs,close();
con。close();
}

#3


引用 2 楼 xuemingyuan88 的回复:
try{                        Connection con = DriverManager.getConnection(                        "jdbc:mysql://localhost:3306/AssetsManageSystem","root","lhakuma");                        System.out.println("数据库服务器连接成功");                        java.sql.Statement stmt = con.createStatement();                         ResultSet rs = stmt.executeQuery("select * from user");                         while(rs.next()){                            if(textField.getText().equals(rs.getString("name"))&&passwordField.getText().equals(rs.getString("pass"))){                                new MainFrame();                                break;                            }                                                         else{                                new JOptionPane().showMessageDialog(null,"用户名或者密码错误!请重新输入");                            }                                                     }                    }                    catch(Exception e2){                        System.out.println("获取数据失败");                        e2.printStackTrace();                    }
finally
{
 // 结果集 连接释放 
rs,close();
con。close();
}


我加了那两句话,可是。。。。 连接数据库的代码出现死循环,求大神相助。出现这个错误时怎么回事??

#4


rs定义在try块外面