2017-03-17 70 views
-1

錯誤:javax.servlet.ServletException:值java.sql.SQLException:列 'NUM' 未找到

this is the error code

代碼:

<% 
String driver = "com.mysql.jdbc.Driver";  
Class.forName(driver).newInstance(); 
    Connection con=null; 
    ResultSet rst=null; 
     ResultSet rst1=null; 
     ResultSet rst2=null; 
     ResultSet rst3=null;   
    Statement stmt=null; 
     Statement stmt1=null; 
     Statement stmt2=null; 
     Statement stmt3=null; 

    try{ 
     String url="jdbc:mysql://localhost/company?user=root&password=root"; 
     con=DriverManager.getConnection(url); 
     stmt=con.createStatement(); 
       System.out.println("success"); 
    } 
    catch(Exception e){ 
    System.out.println(e.getMessage()); 
     System.out.println("failed"); 
    } 
%> 


<% 

     rst = stmt.executeQuery("select max(num) from invoicename;"); 
    if (rst.next()) { 
     String str = rst.getString("num"); 

     rst1 = stmt.executeQuery("Select sum(price) from invoices where invoiceno='" + str + "';"); 
     if (rst1.next()) { 
      int s = rst1.getInt(1); 
      if (rst1.wasNull()) { 
       s = 0; 
      } 
      stmt.executeUpdate("insert into invoice values('" + str + "',curdate(),curtime(),'" + s + "');"); 
     } 
    } 
    stmt.executeUpdate("insert into invoicename values();"); 



%> 

錯誤:

javax.servlet.ServletException: java.sql.SQLException: Column 'num' not found

我無法在上面的代碼中找到問題。該錯誤已附加在圖像中。

+0

請注意,這個問題的第一個版本是非常垃圾的,並且如果不關閉,它值得downvotes值得。請小心翼翼地寫下你的問題,避免txtspk和乞討。請注意,如果編寫真正的單詞太麻煩了,Stack Overflow可能不適合你。 – halfer

回答

1

您有行String str = rst.getString("num");,但您所做的查詢不會返回任何列名爲num(您正在查詢max(num))。要麼你只是試圖從第一個(也是唯一一個)列結果:

String str = rst.getString(1); 

或更改日查詢到一個名稱返回結果,然後當你想要得到的結果引用該名稱:

rst = stmt.executeQuery("select max(num) as maxnum from invoicename;"); 
if (rst.next()) { 
    String str = rst.getString("maxnum");