[go: up one dir, main page]

0% found this document useful (0 votes)
1 views3 pages

TYBBA java servlet

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 3

TYBBA (CA)

Steps to run JAVA Servlet + jdbc Programs

1. In Eclipse :

Window -> Preferences -> Server ->Runtime Environment


Add
Select Apache Tomcat 10.1
Select the folder where Tomcat is stored
Apply

2. Create New Project


Web -> Dynamic Web Project
Select Generate Web.xml development Descriptor

3. Create Home Page html file

src->webapp->new html file

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="MyForm" action="MyClass">
<input type="submit">
</form>
</body>
</html>

4. Java Resources -> Create New Package


5. In new Package create new Servlet
Servlet Name = MyClass

6. Add mysql / postgresql driver .jar file

Project -> Properties -> Java Build Path -> Libraries


Add External JAR
Select .jar file
Apply
1
Page
7. Type following Code

package Mypackage;
import java.io.*;
import java.io.PrintWriter;
import java.sql.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;

/**
* Servlet implementation class MyClass
*/
public class MyClass extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
private static final String URL =
"jdbc:mysql://localhost:3306/school";
private static final String USER = "root";
private static final String PASSWORD = "pmd123";

public MyClass() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
response.getWriter().append("Served at:
").append(request.getContextPath());

response.setContentType("text/html");
PrintWriter out = response.getWriter();

try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(URL,
USER, PASSWORD);
2

String query="insert into student values(?,?)";


Page
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setInt(1, 1);
stmt.setString(2, "Ram");

stmt.executeUpdate();

out.println("Connected");
query="SELECT * FROM student";
Statement st=conn.createStatement();
ResultSet rs = st.executeQuery(query);

out.println("<h2>Database Records:</h2>");
while (rs.next()) {
out.println("<p>Roll: " + rs.getInt("Roll") + ",
Name: " + rs.getString("Name") + "</p>");
}

rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
out.println("<h3>Error: " + e.getMessage() + "</h3>");
}
}

/**
* @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);
}

8. Save and Click on Project Name

Run -> Run As -> Run on Server

Webpage will open in Chrome -> Click on submit


3
Page

You might also like