2014-03-31 74 views
0

嗨,朋友我正在運行一個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) 

請幫我澄清這個問題....

+0

使用* Prepared Statements *而不是像這樣一起修改你的查詢。這很容易 –

+0

顯示您的jsp頁面,並且您不遵循Java命名約定,請遵循。 – Rembo

+1

並學習如何正確縮進代碼 – developerwjk

回答

0

添加此作爲一個答案 -

您需要修復您的jsp中db的定義。您創建的類是dbConn而不是dbConn1

相關問題