Chan

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Servlet

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

<?xml version="1.0" encoding="UTF-8"?>


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>TestSample1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>MyServlet</display-name>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.src.pkg.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
</web-app>
Index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1> Welcome to Presidency Library Page</h1>
<form action="MyServlet">

<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>

<tr><td align="center"><input type="submit"/></td></tr>


</table>

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

DBMS :

create database testdatabase;

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;

SHOW VARIABLES WHERE Variable_name=‘port’;

SELECT user,authentication _string,plugin,host FROM mysql.user;

select* from library;

You might also like