Chan
Chan
Chan
package com.src.pkg;
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.ResultSetMetaData;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MyServlet
*/
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MyServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
try{
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/testdatabase","root",
"Abc12345");
PreparedStatement st = con.prepareStatement("insert into
library values(?, ?, ?, ?, ?, ?)");
st.setInt(1, Integer.valueOf(request.getParameter("rollno")));
st.setString(2, request.getParameter("name"));
st.setString(3, request.getParameter("title"));
st.setString(4, request.getParameter("author"));
st.setInt(5, Integer.valueOf(request.getParameter("price")));
st.setInt(6, Integer.valueOf(request.getParameter("qty")));
st.executeUpdate();
st.close();
con.close();
out.println("<html><body><b>Successfully Inserted"
+ "</b></body></html>");
}catch (Exception e2) {
e2.printStackTrace();
}
finally{
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
Xml
<table>
<tr><td>Enter Roll No:</td>
<td><input type="text" name="rollno"/></td>
</tr>
<tr><td>Enter Name:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr><td>Title:</td>
<td><input type="text" name="title"/></td>
</tr>
<tr><td>Author:</td>
<td><input type="text" name="author"/></td>
</tr>
<tr><td>Price:</td>
<td><input type="text" name="price"/></td>
</tr>
<tr><td>Qty:</td>
<td><input type="text" name="qty"/></td>
</tr>
</form>
</body>
</html>
DBMS :
show databases;
use testdatabase;
create table library(Id integer Primary key,Name varchar(100),title varchar(100),author
varchar(100),price integer,qty integer);
desc library;