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   

JAVA_Full_Stack

  Steps to Ensure Compatibility and Optimal Setup: Download and Install Eclipse : Go to the Eclipse Downloads page and select the "Eclipse IDE for Enterprise Java and Web Developers" package. https://www.eclipse.org/downloads/packages/release/2023-12/r   Download and Install JDK 17 : Obtain JDK 17 from the Oracle JDK Downloads or the OpenJDK 17 page. Follow the installation instructions specific to your operating system. Download and Install Apache Tomcat 10.1 : Download Tomcat 10.1 from the Apache Tomcat Downloads page. Extract the files to a convenient directory. Configure Eclipse to Use JDK 17 : Open Eclipse and navigate to Window > Preferences > Java > Installed JREs . Click Add , select Standard VM , and point it to the JDK 17 installation directory. Set JDK 17 as the default JRE. Configure Tomcat in Eclipse : Open Eclipse and go to Window > Preferences > Server > Runtime Environments . Click Add , select Apache Tomcat v10.0 , and point it to the di...

Tableau

TABLEAU EXPERIMENTS Consider the Dhoni Dataset  click here  solve the following experiments using Tableau Tool Here are a few experiment ideas for students to create visualizations in Tableau using the cricket dataset: Experiment 1: Runs vs. Opponent Objective:  Visualize how the player's performance in terms of runs varies against different opponents. Task:  Create a bar chart showing the total runs scored against each opposing team. Additional Insight:  Add a color gradient to highlight highest to lowest run totals. Experiment 2: Performance Over Time Objective:  Analyze the player’s performance trends over the course of the season. Task:  Develop a line chart that plots runs scored over time (by date). Additional Insight:  Include a dual-axis to plot catches and stumpings alongside runs. Experiment 3: Heatmap of Performances by Stadium Objective:  Compare the player’s performance at different stadiums. Task:  Create a heatmap to displ...