Skip to main content

Connection_Demo

 Java Servlet Database Connectivity


import jakarta.servlet.ServletException;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


/**

* Servlet implementation class Connection_Demo

*/

public class Connection_Demo extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

final String DB_URL = "jdbc:mysql://localhost:3306/demo";


// Database credentials

final String USER = "root";

final String PASS = "Rajesh@2023";


// Set response content type

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("Servlet loaded");

try {

// Register JDBC driver

Class.forName(JDBC_DRIVER);

// Open a connection

Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);

if (conn == null)

out.println("No conenction established");

else

out.println("Conenction established");

// Execute SQL query

Statement stmt = conn.createStatement();

String sql;

sql = "SELECT name,password,email FROM users";

ResultSet rs = stmt.executeQuery(sql);


// Extract data from result set

// out.print("Roll No Name Percentage Address");

out.println("<table border=2><th>Name </th><th>Password</th><th>Email</th>");

while (rs.next()) {

// Retrieve by column name

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

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

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


// Display values

out.print("<tr><td>" + name + "</td><td>" + password + "</td><td>" + email + "</td> <br>");

}

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

// Clean-up environment

rs.close();

stmt.close();

conn.close();

} catch (SQLException se) {

// Handle errors for JDBC

se.printStackTrace();

} catch (Exception e) {

// Handle errors for Class.forName

e.printStackTrace();

}

}




}

Comments

Popular posts from this blog

Data Visualization

Welcome to Data Visualization Lab  B.Tech Information Technology, II Year I Semester, Section C Softwares for Installation Test Link  https://forms.gle/Fnh79CyodToWTGBj7 RStudio Link:  https://posit.co/download/rstudio-desktop/   Power BI Link https://www.microsoft.com/en-us/download/details.aspx?id=58494 Tableau Desktop https://www.tableau.com/products/desktop/download  Week-1 Experiments List https://drive.google.com/file/d/1UlRKXTY9OdK7QyVLa7H-_dmaJkimuecs/view?usp=drive_link  W3Schools -> R Programming  https://www.w3schools.com/R/ Experiments List List of Experiments 1: Programming Practise in R  - Click Here List of Experiments 2 :  Click Here List of Experiments 3 : Click Here   

Data Mining - Assignment & ELA Test-1

  Table – 1 Groceries Dataset Transaction ID Items T1 Milk, Bread, Butter, Eggs, Sugar, Tea, Coffee, Biscuit, Cheese, Yogurt T2 Bread, Butter, Jam, Eggs, Milk, Sugar, Biscuit, Tea, Coffee, Yogurt T3 Bread, Butter, Cheese, Jam, Eggs, Milk, Yogurt, Biscuit, Tea, Coffee T4 Milk, Bread, Cheese, Butter, Sugar, Eggs, Coffee, Tea, Yogurt, Biscuit T5 Butter, Bread, Jam, Milk, Sugar, Coffee, Biscuit, Eggs, Yogurt, Tea T6 Milk, Butter, Cheese, Sugar, Biscuit, Yogurt, Eggs, Bread, Coffee, Tea T7 Bread, Jam, Milk, Butter, Eggs, Biscuit, Coffee, Yogurt, Sugar, Tea T8 Butter, Cheese, Bread, Eggs, Milk, Biscuit, Sugar, Tea, Coffee, Yogurt T9 Bread, Butter, Jam, Cheese, Yogurt, Biscuit, Coffee, Tea, Eggs, Milk T10 Milk, Bread, Butter...

DBMS001

DBMS Practice Experiment   Design 7 tables as per the given requirements, with at least 10 rows per table, including primary keys and foreign keys wherever appropriate. Your tables should be as follows (attributes are suggested but you may modify them if needed):   Table: Department Attribute Data Type Description dept_id (PK) INT Unique ID for department dept_name VARCHAR(50) Name of the department hod_name VARCHAR(50) Head of department name office_location VARCHAR(50) Location of the department office contact_email VARCHAR(50) Department email contact_phone VARCHAR(15) ...