javax.servlet.ServletException:java.sql.SQLException:Column' num'未找到

时间:2017-03-17 12:46:57

标签: jsp

错误:

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&#39; num&#39;找不到

我无法在上面的代码中找到问题。错误已附加在图像中。

1 个答案:

答案 0 :(得分: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");