I am learning JSP, and have developed a books application, which displays the book-id,title,author and price. There are seperate text box for each of the field, whenever the user fills the form and hits submit button the value must be inserted into my database. If I try to submit it to the database nothing is happening. Please look at the following code.

<%-- index.jsp--%>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <title>Online Books Manager</title>

</head>
<body>
    <p>Please Enter the Details in the Necessary field:</p>
    <form method="post" action="insertdb.jsp" id="test" name="test">
        <label for="book_id">book_id:</label>
        <input type="text" name="book_id" placeholder="Please Enter the book Id"  /><br />
        <label for="title">title:</label>
        <input type="text" name="title" placeholder="Title of the Book"/><br />
        <label for="author">author:</label>
        <input type="text" name="author" placeholder="Name of the Author" /><br />
        <label for="price">price:</label>
        <input type="text" name="price" placeholder="Price of the book" /><br />
        <input type="submit" name="Upload" />
    </form>
</body>
</html>



<%-- insertdb.jsp --%>
<%@ page import="java.sql.*" %>
<html>
<head>
    <title>Inserting the values into the database</title> 
</head>
<body>
    <% out.println("Wait while inserting the values in to the database!");
    String JDBC_DRIVER="com.mysql.jdbc.Driver";
    String DB_URL="jdbc:mysql://localhost/practiseDB";
    String User="root";
    String Pass="password";
    Connection con = null;
    Statement st= null;
    ResultSet rs= null;
    try{
        Class.forName("com.mysql.jdbc.Driver");
        out.println("Connecting to Database!");
        con= DriverManager.getConnection(DB_URL,User,Pass);
        st= con.createStatement();
    }
    catch (Exception e){
        System.out.println(e.getMessage());
    }
    if(request.getParameter("action")!=null){
        int book_id= request.getParameter("book_id");
        String title = request.getParameter("title");
        String author= request.getParameter("author");
        int price= request.getParameter("price");
        st.executeUpdate("insert into books_details(book_id,title,author,price)     values('"+book_id+"','"+title+"','"+author+"','"+price+"')");
        out.println("Successfully Inserted");
        rs.close();
        st.close();
        con.close();
    }
    %>
</body>
</html>

Some times I am getting the error in

int book_id= request.getParameter("book_id"); and int price= request.getParameter("price");

So how do code to insert the form values into my database, can anyone please help me ?

Thanking You

Recommended Answers

All 4 Replies

Member Avatar for LastMitch

If I try to submit it to the database nothing is happening. Please look at the following code.

@Varunkrishna

What error did you get? Post your error.

@LastMitch
This is the error I am getting when I try to click the submit button.
org.apache.jasper.JasperException:Unable to Compile class for JSP
An error occured at line No:50 and 53(refer the above code).

Type Mismatch cannot convert from String to int.

Thanking You

can you try this:

int price = 0;
try {
    price = Integer.parseInt(request.getParameter("price"));
} catch(NumberFormatException e) {
    // log the error or ignore it
    // Ex: price = -1;
}

...
int book_id= Integer.parseInt(request.getParameter("book_id"));
...
price = Integer.parseInt(request.getParameter("price"));
....

I had this to fix the error by changing the original code to the above mentioned code, but out of luck, what I am seeing in the web page is that the String generated by the following statement

<% out.println("Wait while inserting the values in to the database!");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.