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