Results 1 to 3 of 3
  1. #1
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default Inserting data to database from the form

    Hi experts..
    i am trying to insert data in to MySQL database from html forms using JSP.Here is the code for it.i am getting the following error which is pasted below.

    <%@ page import="java.sql.*" %>
    <% Class.forName("com.mysql.jdbc.Driver").newInstance (); %>

    <HTML>
    <BODY>

    <FORM ACTION="sample2.jsp" METHOD="POST">
    <BR>
    SSN:<INPUT TYPE="TEXT" NAME="EID">

    <INPUT TYPE="SUBMIT" value="Submit">
    </FORM>

    <H1>Fetching Data From a Database</H1>

    <%
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/EMP","","");

    Statement statement = connection.createStatement();

    String sn = request.getParameter("EID");


    String command = "INSERT INTO EmployeeAddressTable (SSN) VALUES ('sn')";
    statement.executeUpdate(command);


    ResultSet rs = statement.executeQuery("select * from EmployeeAddressTable");
    %>


    <table border="1">
    <tr><td>EID</td>

    </tr>


    <% while (rs.next()) { %>
    <tr>
    <td><%= rs.getString(1) %></td>

    </tr>
    <%}%>


    </table>
    <BR>
    rs.close();
    </BODY>
    </HTML>

    Error:

    type Exception report

    message Internal Server Error

    description The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.

    exception

    javax.servlet.ServletException: Incorrect integer value: 'sn' for column 'SSN' at row 1
    at org.apache.jasper.runtime.PageContextImpl.handlePa geException(PageContextImpl.java:463)
    at org.apache.jsp.sample2$jsp._jspService(sample2$jsp .java:110)
    at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:107)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:853)
    at org.apache.jasper.servlet.JspServlet$JspServletWra pper.service(JspServlet.java:202)
    at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:382)
    at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:474)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:853)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:247)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:243)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:566)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:472)
    at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:943)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:190)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:566)
    at org.apache.catalina.valves.CertificatesValve.invok e(CertificatesValve.java:246)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:564)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:472)
    at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:943)
    at org.apache.catalina.core.StandardContext.invoke(St andardContext.java:2343)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:180)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:566)
    at org.apache.catalina.valves.ErrorDispatcherValve.in voke(ErrorDispatcherValve.java:170)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:564)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:170)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:564)
    at org.apache.catalina.valves.AccessLogValve.invoke(A ccessLogValve.java:468)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:564)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:472)
    at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:943)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:174)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:566)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:472)
    at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:943)
    at org.apache.catalina.connector.http.HttpProcessor.p rocess(HttpProcessor.java:1012)
    at org.apache.catalina.connector.http.HttpProcessor.r un(HttpProcessor.java:1107)
    at java.lang.Thread.run(Thread.java:595)


    root cause

    java.sql.SQLException: Incorrect integer value: 'sn' for column 'SSN' at row 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:946)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:2985)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:16 31)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java :1723)
    at com.mysql.jdbc.Connection.execSQL(Connection.java: 3277)
    at com.mysql.jdbc.Statement.executeUpdate(Statement.j ava:1402)
    at com.mysql.jdbc.Statement.executeUpdate(Statement.j ava:1317)
    at org.apache.jsp.sample2$jsp._jspService(sample2$jsp .java:75)
    at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:107)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:853)
    at org.apache.jasper.servlet.JspServlet$JspServletWra pper.service(JspServlet.java:202)
    at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:382)
    at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:474)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:853)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:247)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:243)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:566)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:472)
    at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:943)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:190)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:566)
    at org.apache.catalina.valves.CertificatesValve.invok e(CertificatesValve.java:246)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:564)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:472)
    at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:943)
    at org.apache.catalina.core.StandardContext.invoke(St andardContext.java:2343)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:180)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:566)
    at org.apache.catalina.valves.ErrorDispatcherValve.in voke(ErrorDispatcherValve.java:170)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:564)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:170)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:564)
    at org.apache.catalina.valves.AccessLogValve.invoke(A ccessLogValve.java:468)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:564)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:472)
    at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:943)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:174)
    at org.apache.catalina.core.StandardPipeline.invokeNe xt(StandardPipeline.java:566)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:472)
    at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:943)
    at org.apache.catalina.connector.http.HttpProcessor.p rocess(HttpProcessor.java:1012)
    at org.apache.catalina.connector.http.HttpProcessor.r un(HttpProcessor.java:1107)
    at java.lang.Thread.run(Thread.java:595)

    as far as i understood the error is due considering EID as string value??? but tried putting it as int..still i get error..

    if anything is wrong..please do correct me..

    Thankyou..

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Java Code:
    "INSERT INTO EmployeeAddressTable (SSN) VALUES ('sn')";
    Java Code:
    java.sql.SQLException: Incorrect integer value: 'sn' for column 'SSN' at row 1
    What did you expect? The program can only do what you tell it to do, and when you tell it to use the String "sn", rather than accessing a variable that you reference as sn, it will do so.

    In any case:

    1. Use a PreparedStatement, don't cobble together a Statement with +'s unless you want an SQL Injection Attack and error-prone, fragile code.

    2. Don't use scriptlets in JSP. They are strongly discouraged, and only still allowed for backward comnpatability reasons. Create a Bean, or some other external Object to do this.

    3. JSP are only for display. POST requests should go to a Servlet.

  3. #3
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 02-07-2008, 08:57 AM
  2. Inserting data containing quotes into DB
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-06-2008, 09:28 AM
  3. Inserting file in to database
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-07-2008, 08:39 AM
  4. Replies: 0
    Last Post: 12-24-2007, 09:47 AM
  5. how to upload a file along with html form data
    By pranith in forum Java Servlet
    Replies: 3
    Last Post: 07-30-2007, 02:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •