2015-03-31 52 views
0

我是新来的Java和MySQL,我目前正在努力完成我的任务,虽然我有一个IF函数的问题。我想根据客户ID将查询结果从MySQL打印到控制台,如果客户ID不存在,则显示错误消息。Java如果 - 否则功能不能正常工作

如果在数据库中找到ID,则显示数据,但如果ID不存在,则不会发生任何情况。任何帮助将不胜感激!

package cos106_assignment_april2015; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
import java.sql.*; 
import javax.swing.border.*; 


public class ProductSearch { 

JFrame f = new JFrame("Ray Sport Inc. | Product Search"); 
JTextField custIDF; 

public ProductSearch() { 
    JLabel search1, customer, empty; 
    search1 = new JLabel("Enter Customer ID number to search for a customer's product records. Results print to console."); 
    customer = new JLabel("Customer ID:"); 
    empty = new JLabel(""); 

    custIDF = new JTextField(); 

    JButton search, back; 
    search = new JButton("Search"); 
    back = new JButton("Back to Menu"); 

    search.addActionListener(new ActionListener() { 
    public void actionPerformed (ActionEvent e){ 

    String custID = custIDF.getText(); 

    try { 
    System.out.println("Establishing connection to the MySQL Database..."); 
    Connection conn = null; 
    Statement stmt =null; 
    String url = "jdbc:mysql://localhost:3306/raysportdb"; 
    String driver = "com.mysql.jdbc.Driver"; 
    String userName = "root"; 
    String password = "password"; 
    Class.forName(driver).newInstance(); 
    conn = DriverManager.getConnection(url,userName,password); 
    stmt = conn.createStatement(); 
    System.out.println("Connection established. Connected to the customer and item records."); 

     String sql = "select a.Customer_ID, c.Order_ID, b.Product_ID, \n" + 
        "b.Product_Name, b.Product_Type, c.Order_Quantity \n" + 
        "from customer a, product b, orders c \n" + 
        "where a.Customer_ID = '"+custID+"' and b.Product_ID = c.Product_ID \n" + 
        "and b.Product_Type = c.Product_Type_FK and a.Customer_ID = c.Customer_ID \n" + 
        "group by c.Order_ID order by a.Customer_ID"; 

     ResultSet result = stmt.executeQuery(sql); 
     while(result.next()){ 
      String cid = result.getString("a.Customer_ID"); 
      if (custID.equals (cid)) { 

       String f2 = result.getString("c.Order_ID"); 
       String f3= result.getString("b.Product_ID"); 
       String f4 = result.getString("b.Product_Name"); 
       String f5 = result.getString("b.Product_Type"); 
       String f6 = result.getString("c.Order_Quantity"); 

       System.out.println(""); 
       System.out.println("Customer ID\t:"+ cid); 
       System.out.println("Order ID\t:" + f2); 
       System.out.println("Product ID\t:" + f3); 
       System.out.println("Product Name\t:" + f4); 
       System.out.println("Product Type\t:" + f5); 
       System.out.println("Order Quantity\t:" + f6); 
       System.out.println(""); 
            } 
      else { 
       JOptionPane.showMessageDialog(null, "Customer ID does not exist!", "Error", JOptionPane.ERROR_MESSAGE); 
       System.out.println("Customer ID does not exist in the database."); 
       } 
      } 
     conn.close(); 
    } 

    catch (Exception err) { 
     System.err.println("Error:"+ err.getMessage()); 
          }}}); 


    back.addActionListener(new ActionListener() { 
    public void actionPerformed (ActionEvent e){ 
    new Menu(); 
    f.setVisible(false);}}); 

    JPanel p = new JPanel(); 
    p.setLayout(new GridLayout(6,2)); 
    p.setBorder(new TitledBorder("Product Search")); 
    p.add(search1); 
    p.add(empty); 
    p.add(customer); 
    p.add(custIDF); 
    p.add(search); 
    p.add(back); 

    f.getContentPane().add(p); 
    f.pack(); 
    f.setSize(585,284); 
    f.setLocationRelativeTo(null); 
    f.setVisible(true); 
    f.setResizable(false); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 



} 

public static void main(String ar[]){ 

new ProductSearch(); 
} 
} 
+1

您的'if'在循环中。如果你得到了多个结果,你的'else'将为每个与你要查找的id不匹配的对象激发。 – khelwood 2015-03-31 13:39:57

+0

我怀疑if-else语句什么都不做。它看起来更多,你没有得到任何结果。另外,您应该在执行sql查询时尝试检查[PreperedStatement](http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html) – SomeJavaGuy 2015-03-31 13:40:25

回答

0

你必须在你的while循环之外粘贴你的代码。当你的查询没有返回结果时,它将永远不会遍历你的循环。您可以执行以下操作:

if (result.next()){ 
// your code goes here 
} else { 

JOptionPane.showMessageDialog(null, "Customer ID does not exist!", "Error", JOptionPane.ERROR_MESSAGE); 
System.out.println("Customer ID does not exist in the database."); 
} 
0

你如果结果对象为空(NOT NULL)while(result.next()){前检查。 如前所述通过Java文档:

boolean next() 
      throws SQLException 

从当前位置向前移一行移动光标。 A ResultSet光标最初位于第一行之前;首先调用下一个方法使第一行成为当前行;第二次调用 使第二行成为当前行,依此类推。

事实上,如果发现任何结果执行查询你永远也做不到的else 部分。