0% found this document useful (0 votes)
11 views4 pages

Start DATABASE W - Servlet

The document outlines the creation of a MySQL database named 'testdb' and a 'users' table with fields for id, name, and email. It also includes a Java servlet that connects to the database, retrieves user data, and displays it in an HTML table format. Additionally, it specifies the necessary MySQL JDBC dependency for the project.

Uploaded by

mamomohi13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Start DATABASE W - Servlet

The document outlines the creation of a MySQL database named 'testdb' and a 'users' table with fields for id, name, and email. It also includes a Java servlet that connects to the database, retrieves user data, and displays it in an HTML table format. Additionally, it specifies the necessary MySQL JDBC dependency for the project.

Uploaded by

mamomohi13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

//start CREATE DATABASE testdb;

USE testdb;

CREATE TABLE users (

id INT AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(100) NOT NULL,

email VARCHAR(100) NOT NULL

);

//html

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>8.0.26</version>

</dependency>

//servlet project

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.SQLException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@WebServlet("/users")

public class UserServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

// Database connection details

private static final String JDBC_URL = "jdbc:mysql://localhost:3306/testdb";

private static final String JDBC_USERNAME = "root";

private static final String JDBC_PASSWORD = "yourpassword";

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

Connection conn = null;


PreparedStatement pstmt = null;

ResultSet rs = null;

try {

// Load the MySQL JDBC driver

Class.forName("com.mysql.cj.jdbc.Driver");

// Establish the connection

conn = DriverManager.getConnection(JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD);

// Create a SQL query

String sql = "SELECT * FROM users";

pstmt = conn.prepareStatement(sql);

// Execute the query

rs = pstmt.executeQuery();

// Display the results

out.println("<html><body>");

out.println("<h2>Users</h2>");

out.println("<table border='1'><tr><th>ID</th><th>Name</th><th>Email</th></tr>");

while (rs.next()) {

int id = rs.getInt("id");

String name = rs.getString("name");


String email = rs.getString("email");

out.println("<tr><td>" + id + "</td><td>" + name + "</td><td>" + email + "</td></tr>");

out.println("</table></body></html>");

} catch (Exception e) {

e.printStackTrace(out);

} finally {

// Close the resources

try {

if (rs != null) rs.close();

if (pstmt != null) pstmt.close();

if (conn != null) conn.close();

} catch (SQLException e) {

e.printStackTrace(out);

You might also like