2013-01-20 86 views
-1

執行以下代碼,直到String check =「select * from borrow where librarycardnumber =?」;在單擊鼠標後在try {} catch {}塊中,然後在控制檯中沒有任何錯誤地停止。可能是什麼問題?準備聲明或其他什麼是錯誤的?謝謝。準備語句不會在java中的鼠標事件後執行

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){ 


       } 

      } 
     }); 

    } 

} 
+0

通過編輯問題給出異常的整個堆棧跟蹤。 – TechSpellBound

+0

你在lcfield中輸入什麼內容? – TechSpellBound

+0

在控制檯上的setString()之後打印'stmt',並根據語法查看生成的sql是否正確。你有創建借用表嗎? –

回答

2

該代碼的最大問題是你捕獲一個異常,並在catch塊中什麼都不做。你必須至少做一個e.printStackTrace()這將幫助你弄清楚發生了什麼。

+0

謝謝,它說我在SQL語法中有一個錯誤。這可能是一個問號。 –