2017-03-17 71 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");