-1

执行以下代码,直到 String check = "select * from borrow where librarycardnumber = ?"; 在单击鼠标后的 try{} catch{} 块中,然后在控制台中停止而没有任何错误。可能是什么问题?是错误的准备语句还是其他?谢谢你。

import javax.swing.*;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import javax.swing.JTextField;
import java.awt.event.MouseEvent;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class libaryCard extends JFrame{

    public JLabel lclabel;
    public JTextField lcfield;
    public JButton lcbutton;

    public libaryCard(){
        super("Library Card Check");

        setLayout(new FlowLayout());
        setSize(300, 130);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        lclabel = new JLabel("Enter your Library Card Number:");
        add(lclabel);

        lcfield = new JTextField(20);
        add(lcfield);

        lcbutton = new JButton("Check");
        add(lcbutton);
        lcbutton.addMouseListener(new MouseAdapter() {


            @Override

            public void mouseClicked(MouseEvent e) {
                System.out.println("mouse clicked");
                try{

                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                    java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+libaryCheck.DBname+"", "root", libaryCheck.DBpass);
                    System.out.println("Connected ");
                    String LB = lcfield.getText();
                    PreparedStatement stmt= null;
                    ResultSet libCheck = null;
                    String check = "select * from borrow where librarycardnumber = ?";

                    System.out.print("String executed");

                    stmt=conn.prepareStatement(check);
                    stmt.setString(1, LB);
                    libCheck = stmt.executeQuery(check);


                    System.out.println("Query executed");

                    if(libCheck.next()) {
                        String number = libCheck.getString("libraryCardNumber");

                        ISBNcheck isbn= new ISBNcheck();
                        isbn.setVisible(true);

                        System.out.println("mouse action complete");
                    }
                    else {

                        JOptionPane.showInputDialog("Card Number not found!");
                    }

                } catch (Exception ed){


                }

            }
        });

    }

}
4

1 回答 1

2

代码的最大问题是您正在捕获异常并且在 catch 块中什么都不做。你至少必须做一个e.printStackTrace()可以帮助你弄清楚发生了什么的事情。

于 2013-01-20T22:13:36.730 回答