【问题标题】:How to insert data into oracle 11g DBMS through html form using Eclipse?如何使用 Eclipse 通过 html 表单将数据插入 oracle 11g DBMS?
【发布时间】:2017-10-21 18:16:22
【问题描述】:

运行java程序时出错-->>

我在 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();
    }
    }
}

Web.xml 也已链接,我已将 servlet 链接到项目

<?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>

【问题讨论】:

  • 为什么你没有在 doPost 方法的第三行初始化“customerId”
  • 错误的哪一部分让您感到困惑?如果从未进入while(rs.next()) 循环,customerId 将具有什么值?编译器不知道 SELECT MAX(...) 将始终只返回一行,因此就编译器所知,while 块可能永远不会运行。

标签: java html eclipse oracle servlets


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-15
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 2021-11-14
    • 1970-01-01
    • 2014-08-20
    相关资源
    最近更新 更多