Results 1 to 5 of 5
  1. #1
    gb.rashu is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Question data is not inserting into database

    Hello,
    I am inserting data into the database using mysql and jsp. data is inserting as null into database not inserting values . can anyone tell me the solution for this? . am waiting ..

    Here is the code:(.jsp)

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ page language="java" import="java.sql.*;" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <body>
    <center> <h3>Submit Your Resume</h3></center>


    <form enctype="multipart/form-data" method="post" name="theform">
    <table align="center" width="50%" border="0" cellspacing="5" cellpadding="0">
    <tr>
    <td>First Name:</td>
    <td><input type="text" name="firstname"></td>
    </tr>
    <tr>
    <td>Last Name: </td>
    <td><input type="text" name="lastname" /></td>
    </tr>
    <tr>
    <td>Mobile Number: </td>
    <td><input type="text" name="mobilenumber" /></td>
    </tr>


    <tr>
    <td>&nbsp;</td>
    <td><input type="submit" value="submit" name="submit" /></td>
    </tr>
    </table>

    <%
    String firstName=request.getParameter("firstname");
    String lastName=request.getParameter("lastname");
    String mobilenumber=request.getParameter("mobilenumber");

    Connection con=null;
    ResultSet rs=null;
    Statement st=null;
    try
    {
    String dbuser="root";
    String dbpass="admin";
    String url="jdbc:mysql://localhost/gb";

    Class.forName("com.mysql.jdbc.Driver").newInstance ();
    con=DriverManager.getConnection(url, dbuser, dbpass);
    st=con.createStatement();


    //String str="create table person(firstname varchar(40),lastname varchar(50),mobilenumber varchar(50))";
    //st.execute(str);
    String str2="insert into person values('"+firstName+"','"+lastName+"','"+mobilenum ber+"')";
    st.executeUpdate(str2);



    }
    catch(Exception e)
    {
    System.out.println("Can not connect to database");

    }



    %>

    </form>
    </body>
    </html>



    Thanks in advance
    Rashmi

  2. #2
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    try this instead

    Java Code:
    catch(Exception e)
    {
    System.out.println(e);
    }
    and show us what it prints in the console since it will print out the exception that it is throwing.

    also did you notice the space in mobile number

    Java Code:
    String str2="insert into person values('"+firstName+"','"+lastName+"','"+mobilenum ber+"')";
    you could also try this format

    Java Code:
    .executeUpdate("INSERT INTO myTable(test_id, " + "test_val) VALUES(2,'Two')");
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Simply log the sql statement and check values are valid or not. Without debugging it's not possible to track it.

  4. #4
    gb.rashu is offline Member
    Join Date
    Aug 2010
    Posts
    28
    Rep Power
    0

    Default

    i tried ..it is not throwing any exceptions . database connection is executing but null value is inserting into the database.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. inserting date in oracle database
    By jackjosh_in in forum Advanced Java
    Replies: 6
    Last Post: 05-23-2010, 06:38 PM
  2. inserting values from jtable into database
    By sandeepsai17 in forum New To Java
    Replies: 1
    Last Post: 06-29-2009, 07:31 PM
  3. Inserting data to database from the form
    By Ms.Ranjan in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-17-2008, 01:06 AM
  4. Inserting data containing quotes into DB
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-06-2008, 09:28 AM
  5. Inserting file in to database
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-07-2008, 08:39 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
  •