2017-10-21 83 views
-3

错误在运行Java程序时 - >>
PHOTO ATTACHED如何使用Eclipse通过html表单将数据插入到Oracle 11g DBMS中?

我得到一个错误的servlet的编码。这个动态web项目的意图是将表单内容存储到dbms中。 我已经附加了web.xml,servlet,html和这个。需要帮助enter code here

CustomerController.java是servlet的名称。我在图像中指出了错误 。

package com; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
public class CustomerController extends HttpServlet { 


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 

    StringBuilder allHobbies=null; 
    char MarriedStatus; 
    Integer customerId; 

//object creation to utilize jdbc and its utilites 
    Connection conn=null; 
    PreparedStatement pstatement=null; 
    Statement stmt=null; 
    String query =null; 
    ResultSet rs=null; 
    int numberOfRowsInserted=0; 
//reading parameter values from request object provided by user in registration page 
    String firstName=(String)request.getParameter("txtFirstName"); 
    String lastName=(String)request.getParameter("txtLastName"); 
    String contact=(String)request.getParameter("txtContact"); 
    String gender=(String)request.getParameter("radGender"); 
    String city=(String)request.getParameter("cmbcity"); 
    String isMarried=(String)request.getParameter("chkMarried"); 
    String[] hobbies=(String[])request.getParameterValues("SelHobbies"); 

// Initializing connection URL and credentials for database connectivity 
    String url="jdbc:oracle:thin:@localhost:1521:XE"; 
    String userName="system"; 
    String password="lakshmi1"; 

    try 
    { 
     //register the driver class 
     Class.forName("oracle.jdbc.driver.OracleDriver"); 
     //creating the connection object 
     conn=DriverManager.getConnection(url,userName,password); 
     System.out.println("connection established successfully"); 

    // getting value for ismarried field from html 
     if("on".equalsIgnoreCase(isMarried)){ 
      MarriedStatus='Y'; 
     } 
     else{ 
      MarriedStatus='N'; 
     } 

     query="INSERT INTO TBL_CUSTOMER VALUES(CUSTOMER_REG_SEQ2.nextval,?,?,?,?,?,?)"; 
     pstatement= conn.prepareStatement(query); 
     pstatement.setString(1,firstName); 
     pstatement.setString(2,lastName); 
     pstatement.setString(3,contact); 
     pstatement.setString(4,gender); 
     pstatement.setString(5,city); 
     pstatement.setString(6,String.valueOf(MarriedStatus)); 
     numberOfRowsInserted=pstatement.executeUpdate(); 
     conn.commit(); 
     // 
     query="SELECT MAX(CUSTOMER_ID) FROM TBL_CUSTOMER"; 
     stmt=conn.createStatement(); 
     rs=stmt.executeQuery(query); 


     while(rs.next()){ 
      customerId=rs.getInt(1); 
      System.out.println("customerId:"+customerId); 
     } 

     for(int i=0;i<hobbies.length;i++){ 
      query="INSERT INTO tbl1_hobbies VALUE(?,?)"; 
      pstatement=conn.prepareStatement(query); 
      pstatement.setInt(1, customerId); 
      pstatement.setString(2, hobbies[i]); 
      numberOfRowsInserted=pstatement.executeUpdate(); 
      conn.commit(); 
      } 
      PrintWriter out=response.getWriter(); 
      out.println("Registration is successful"); 
      out.println("your customer id is :"+customerId); 

    } 




     catch(SQLException e){ 
     e.printStackTrace(); 
    } 
     catch(ClassNotFoundException e){ 
     System.out.println("Class not found . Add jar files properly"); 
     e.printStackTrace(); 
    } 
    } 
} 

image

Web.xml也被链接,我已发现,小服务程序向项目

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
    <display-name>htmlwithservletDB</display-name> 
    <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
    <welcome-file>default.html</welcome-file> 
    <welcome-file>default.htm</welcome-file> 
    <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 
    <servlet> 
    <description></description> 
    <display-name>CustomerController</display-name> 
    <servlet-name>CustomerController</servlet-name> 
    <servlet-class>com.CustomerController</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>CustomerController</servlet-name> 
    <url-pattern>/CustomerController</url-pattern> 
    </servlet-mapping> 
</web-app> 

的HTML代码被附加以下

<html> 
<head> 
<title>Insert title here</title> 
</head> 
<body> 
    <form method="post" action="CustomerController"> 
    <table border="1"> 
    <tr> 
     <td>First name: </td> 
     <td><input type="text" name="txtFirstName" id="txtFirstName" /> </td> 
    </tr> 
    <tr> 
     <td>Last name: </td> 
     <td><input type="text" name="txtLastName" id="txtLastName" /> </td> 
    </tr> 
    <tr> 
    <td> Contact no: </td> 
    <td> <input type="text" name="txtContact" id="txtContact" /> 
    </td> 
</tr> 
    <tr> 
     <td>Gender: </td> 
     <td><input type="radio" name="radGender" id="radGender" value="M" />MALE 
     <input type="radio" name="radGender" id="radGender" value="F" />FEMALE </td> 
    </tr> 
    <tr> 
     <td>City: </td> 
     <td><select name="cmbcity" id="cmbcity" style="width: 150px; "> 
     <option value="0">--SELECT--</option> 
     <option value="MUMBAI">MUMBAI</option> 
     <option value="CHENNAI"> CHENNAI</option> 
     <option value="KOLKATA"> KOLKATA</option> 
     <option value="DELHI"> DELHI</option> 
     </select> 
     </td> 
     </tr> 
    <tr> 
    <td> Is Married</td> 
    <td><input type="checkbox" name="chkMarried" value="chkMarried" /></td> 
    </tr> 
    <tr> 
<td> HOBBIES </td> 
<td> 
    <select id="SelHobbies" multiple="multiple" size="6" name="SelHobbies"> 
     <option value="Reading">Reading</option> 
     <option value="Sleeping">Sleeping</option> 
     <option value="Singing">Singing</option> 
     <option value="Dancing">Dancing</option> 
     <option value="Painting">Painting</option> 
     <option value="Cooking">Cooking</option> 
     <option value="Citysighting">City sighting</option> 
    </select> 
    </td> 
    </tr> 
    <tr> 
    <td colspan="2" align="center"> 
     <input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" style="width: 121px; color: #000000; text-decoration: underline"/> 
     </td> 
    </tr> 
    </table> 
    </form> 





    </table> 


    </form> 

</body> 
</html> 
+0

为什么你还没有初始化第三行doPost方法中的“customerId” – shivam

+0

哪部分错误令你感到困惑?如果'while(rs.next())'循环没有被输入,'customerId'会有什么值?编译器不知道'SELECT MAX(...)'总是只返回一行,所以就编译器所知,'while'块可能永远不会运行。 – Andreas

回答

0

customerId的值是分配在while循环 - 有可能不会有rs.next()customerId=rs.getInt(1);永远不会执行。因此变量customerId可能不会被初始化。 在while循环之前将其设置为某个值(0或-1或其他值)以停止出现错误。

相关问题