将值插入数据库时​​出错

时间:2014-03-31 16:05:00

标签: sql jsp tomcat

嗨朋友我正在运行一个jsp表单来将值插入数据库。我为数据库连接编写了单独的类。连接代码如下所示

import javax.annotation.Resource;
import java.sql.*;

public class dbConn
  {
    // Member Variables
    private String m_DBLoc = "jdbc:odbc:wipro";
   private String m_DBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; 
    private ResultSet m_RS = null;  // RecordSet Variable
    private Connection m_conn = null; 

 public String setData(String name, String pwd, String fName, String lName, String email, String pno, String address, String state, int pin, String cno)    {
    String sqlInsSt = "INSERT INTO USER_TBS VALUES('"+name+"','"+pwd+"','"+fName+"','"+lName+"','"+email+"','"+pno+"','"+address+"','"+state+"',"+pin+",'"+cno+"')" ;
    int n = 0;
        if(m_conn == null) // if Connection has not been set
        return "Connection failed" ;

    try {
             Statement  s = m_conn.createStatement();
             n = s.executeUpdate(sqlInsSt);
            }catch (SQLException e) {
            e.printStackTrace();} // if a SQL error occurs

        if(n !=0) 
        return "Data inserted successfully" ;
    else
        return "Data insertion is failed" ;
    }   

public ResultSet getData()  {
    String sqlStatement = "SELECT pno FROM new_connection" ;
        ResultSet temp = executeQuery(sqlStatement);    
    return temp;        
    }

   //-------------------------------------------------------
    // Method executeQuery -- for executing queries.  Returns 
    //  a ResultSet (RecordSet) kind of like in ADO
    //-------------------------------------------------------
    public ResultSet executeQuery(String stmt)
    { 
        if(m_conn == null) // if Connection has not been set
          m_RS = null; 
        else
        {  try {
             Statement  s = m_conn.createStatement();
             m_RS = s.executeQuery(stmt);
           }
           catch (SQLException e) {e.printStackTrace();} // if a SQL error occurs
        }
        return(m_RS);
    }

//-----------------------------------------------------
    // Method DBConnect -- must connect to the DB before a 
    //  query can be executed.  Returns an error message
    //  to be printed by the caller if there is an error.
    //-----------------------------------------------------
    public String DBConnect()
    { 
      String retVal = ""; // there are no errors yet
      //Connection conn = null;
      try // try to connect to the database
      {  Class.forName(m_DBDriver);
         m_conn = DriverManager.getConnection(m_DBLoc,"scott","tiger");
      } 
      // if the driver class isn't found
      catch (ClassNotFoundException e) {retVal = e.toString();
      e.printStackTrace();}
      catch (SQLException e) {retVal = e.toString();
      e.printStackTrace();} // if a SQL error occurs

      return(retVal); // returns error messages or an empty string 
                      // that the caller must print.
    }
}

当我将值插入数据库时​​,会显示以下错误

type Exception report

message Unable to compile class for JSP:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: [15] in the generated java file: [H:\apache-tomcat-8.0.3\work\Catalina\localhost\hello\org\apache\jsp\telecom\_6\newconnexp_jsp.java]
Only a type can be imported. chandu.dbConn1 resolves to a package

An error occurred at line: 10 in the jsp file: /telecom/6/newconnexp.jsp
dbConn1 cannot be resolved to a type
7: 
8: <%! 
9:     ResultSet rs ;
10:     dbConn1 db ;
11:     String sConn;
12:     public void jspInit() {
13:         db = new dbConn1();


An error occurred at line: 13 in the jsp file: /telecom/6/newconnexp.jsp
db cannot be resolved to a variable
10:     dbConn1 db ;
11:     String sConn;
12:     public void jspInit() {
13:         db = new dbConn1();
14:         sConn = db.DBConnect();
15:         rs = null; 
16:     }


An error occurred at line: 13 in the jsp file: /telecom/6/newconnexp.jsp
dbConn1 cannot be resolved to a type
10:     dbConn1 db ;
11:     String sConn;
12:     public void jspInit() {
13:         db = new dbConn1();
14:         sConn = db.DBConnect();
15:         rs = null; 
16:     }


An error occurred at line: 14 in the jsp file: /telecom/6/newconnexp.jsp
db cannot be resolved
11:     String sConn;
12:     public void jspInit() {
13:         db = new dbConn1();
14:         sConn = db.DBConnect();
15:         rs = null; 
16:     }
17: %>


An error occurred at line: 39 in the jsp file: /telecom/6/newconnexp.jsp
db cannot be resolved
36:         
37:         String sInsState =null;
38:         if(sConn.equals("")) {
39:             sInsState = db.setData(uname, cname, occupation, purpose, gender, income, dob, address, state, pin);
40:         }
41: %>
42: <%String x=sInsState;%>


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:199)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:467)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:380)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:355)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:342)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:403)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:347)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

请帮我澄清问题....

1 个答案:

答案 0 :(得分:0)

添加此作为答案 -

您需要在jsp中修复db的定义。您创建的课程为dbConn而不是dbConn1