2013-07-15 182 views
0

我試圖編寫一個代碼來搜索學生的數據庫,並在選項窗格上顯示搜索結果。最後我寫了以下內容。預期的結果是:如何將字符串傳遞給JOptionPane?

名稱: 「東西」 卷: 「東西」 註冊: 「東西」

但實際上輸出是這樣的:

名稱:null Roll:null 註冊:null


public class Search 

{

JFrame sw = new JFrame("Search Students' Info"); //search window 
JTable stable; 
JLabel stfl = new JLabel("Roll");     //search text field label 
JTextField stf = new JTextField(8);    //search text field 


JButton sb = new JButton("Search");    //search button 

public void exsearch()        //Execute Search 
{ 
    sw.setLayout(new GridLayout(2,2)); 
    sw.add(stfl); 
    sw.add(stf); 
    sw.add(sb); 


    sw.setSize(200, 100); 
    sw.setLocation(100, 100); 
    sw.setVisible(true); 

    sb.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      String driver = "com.mysql.jdbc.Driver"; 
      String url = "jdbc:mysql://localhost/srsdb"; 
      try 
      { 
       Class.forName(driver).newInstance(); 

       java.sql.Connection con = DriverManager.getConnection(url, "root", ""); 
       JOptionPane.showMessageDialog(null, "Connected", "Connection Confirmation", JOptionPane.PLAIN_MESSAGE); 

       String str ="SELECT* FROM students WHERE Roll="+stf.getText(); 
       java.sql.Statement st = con.createStatement(); 
       java.sql.ResultSet rs = st.executeQuery(str); 

       rs.first(); 

       int n = rs.getMetaData().getColumnCount(); 
      // String[] columnnNames; 
       String[] attributes= new String[10]; 

       int j; 

       while(rs.next()) 
       { 
        for(j=0; j<3; j++) 
        { 
         attributes[j] = rs.getString(j); 
        } 
       } 
        JOptionPane.showMessageDialog(null, "Name :"+attributes[0]+"\nRoll :"+attributes[1]+"\nRegistration :"+attributes[2], "Search Result", JOptionPane.PLAIN_MESSAGE); 
      } 
     catch(Exception f) 
     { 
      f.printStackTrace(); 
      JOptionPane.showMessageDialog(null, "Not Found", "Search Result", JOptionPane.PLAIN_MESSAGE); 
     } 

    }}); 


} 

public static void main (String[] args) 
{ 
    new Search(); 
} 

}

+0

這可能意味着查詢不返回任何東西。使用您的調試器逐行執行代碼並檢查查詢和結果。或者將日誌記錄添加到您的代碼中。 –

回答

0

我會用調試器來檢查省略了你的代碼的一部分。 一個奇怪的事情,我看到的是,你跳轉到第一條記錄在結果集中:

rs.first(); 

然後你在一個while循環

while(rs.next()) 
{ 
    for(j=0; j<3; j++) 
    { 
     attributes[j] = rs.getString(j); 
    } 
} 

這可以確保讀取的所有後續記錄數據,如果有多個匹配記錄,您將獲取最後匹配記錄的數據。 更好的辦法是檢查是否有零個或多個記錄。如果是這種情況,請發出警告,因爲可能代碼有問題(使用調試器來查明)。 如果只有一條記錄,從記錄中讀取數據,並打印(或多或少像你嘗試用rs.getString())

0
  1. 的Class.forName確保驅動程序類是類路徑上,而不需要針對課堂進行編譯。它加載類。 無需實例化。
  2. 對於SQL injection,PreparedStatement更好。
  3. SQL API中的列號基於1:1,2,3,... 有點例外。
  4. 使用first時,查詢循環如下。你跳過了我想的第一行。
  5. 別忘了雜項close()
  6. 更好的樣式列出列而不是`*'。

因此:

 String driver = "com.mysql.jdbc.Driver"; 
     String url = "jdbc:mysql://localhost/srsdb"; 
     try 
     { 
      Class.forName(driver); 

      Connection con = DriverManager.getConnection(url, "root", ""); 
      JOptionPane.showMessageDialog(null, "Connected", 
       "Connection Confirmation", JOptionPane.PLAIN_MESSAGE); 

      String str ="SELECT Name, Registration FROM students WHERE Roll=?"; 
      PreparedStatement st = con.createPreparedStatement(str); 

      String roll = stf.getText(); 
      st.setString(1, roll); 

      String message = ""; 
      java.sql.ResultSet rs = st.executeQuery();    
      int recno = 0;  
      if (rs.first()) { 
       do { 
        String name = rs.getString(1); 
        String registration = rs.getString(2); 
        ++recno; 
        message += "- " + recno + " -\nName: " + name 
         + "\nRoll: " + roll 
         + "\nRegistration: " + registration + "\n"; 
       } while (rs.next()); 
      } 
      rs.close(); 

      JOptionPane.showMessageDialog(null, message, "Search Result", 
       JOptionPane.PLAIN_MESSAGE);