0% found this document useful (0 votes)
17 views6 pages

Advance javaPRACTICAL 7

The document outlines the creation of a Java Servlet and a JSP application that connect to MySQL databases, specifically an Admission Database and a Student Database. It includes pseudo code and source code for both applications, detailing steps for establishing connections and handling success or error messages. The servlet and JSP applications are designed to display connection statuses based on the success or failure of the database connections.

Uploaded by

tanishkasingh220
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)
17 views6 pages

Advance javaPRACTICAL 7

The document outlines the creation of a Java Servlet and a JSP application that connect to MySQL databases, specifically an Admission Database and a Student Database. It includes pseudo code and source code for both applications, detailing steps for establishing connections and handling success or error messages. The servlet and JSP applications are designed to display connection statuses based on the success or failure of the database connections.

Uploaded by

tanishkasingh220
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/ 6

NAME:Tanishka singh

Roll no:5355
Class:Sycs(B)
PRACTICAL 7

1 Create a Java Servlet that connects to a MySQL database. The servlet should establish a
connection to the database(Admission Database), and based on the success or failure of
the connection, display an appropriate message

Pseudo code:

1 Open MySql workbench create schema, add tables and add columns

2 download zip file of MySql connector and extract all

3 create servlet click on libraries add JAR file

4 Set database URL, username, and password.

5. Use try block to establish connection using DriverManager.getConnection().

6. Print success or error message.

Source code:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
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("/DatabaseConnectionServlet")
public class data extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String dbURL = "jdbc:mysql://localhost:3306/tanishka";
NAME:Tanishka singh
Roll no:5355
Class:Sycs(B)
String dbUsername = "root"; // Update with your username
String dbPassword = "root75"; // Update with your password
Connection conn = null;
response.setContentType("text/html");
PrintWriter out = response.getWriter();

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

conn = DriverManager.getConnection(dbURL, dbUsername, dbPassword);


out.println("<html><body>");
out.println("<h2>Tanishka singh</h2>");
out.println("<h2>Connection to the Admission Database was successful!</h2>");
out.println("</body></html>");
} catch (ClassNotFoundException e) {
out.println("<html><body>");
out.println("<h3>Error: MySQL JDBC Driver not found!</h3>");
out.println("<p>" + e.getMessage() + "</p>");
out.println("</body></html>");
} catch (SQLException e) {

out.println("<html><body>");
out.println("<h3>Error: Unable to connect to the Admission Database!</h3>");
out.println("<p>" + e.getMessage() + "</p>");
out.println("</body></html>");
} finally {

if (conn != null) {
try {
conn.close();
NAME:Tanishka singh
Roll no:5355
Class:Sycs(B)
} catch (SQLException e) {
out.println("<html><body>");
out.println("<h3>Error closing the database connection!</h3>");
out.println("<p>" + e.getMessage() + "</p>");
out.println("</body></html>");
}
}
}
}
}
Output:
NAME:Tanishka singh
Roll no:5355
Class:Sycs(B)

2 Create JSP application to tests the connection to a MySQL database(Student


Database)and based on the success or failure of the connection, display an appropriate
message

Pseudo code:

1.Import java.sql.* and set content type in JSP.

2. Define database URL, username, and password.

3. Use try catch block to load JDBC driver and establish connection.

4. Display success message if connection is valid; otherwise, display failure message.

5. Handle SQLException in catch.

Source code:

<%@ page import="java.sql.*" %>

<!DOCTYPE html>

<html>

<head>

<title>Check MySQL Connection</title>

</head>

<body>

<h2>MySQL Database Connection Status</h2>

<%

// Database connection details

String url = "jdbc:mysql://localhost:3306/emp"; // Update with your DB URL

String username = "root"; // Update with your DB username

String password = "root75"; // Update with your DB password


NAME:Tanishka singh
Roll no:5355
Class:Sycs(B)
try

// Load MySQL JDBC driver

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

// Establish connection

Connection connection = DriverManager.getConnection(url, username, password);

out.println("<p style='color: green;'>Connection to MySQL database established


successfully!</p>");

// Close connection connection.close(); } catch (Exception

e) { out.println("<p style='color: red;'>Error: " + e.getMessage() +

"</p>");

%>

</body>

</html>

OUTPUT:
NAME:Tanishka singh
Roll no:5355
Class:Sycs(B)

You might also like