0% found this document useful (0 votes)
6 views7 pages

Untitled Document

The document contains Java programs for creating and managing employee and student databases, including creating tables, inserting records, and querying data based on conditions. It also includes servlet code for handling form submissions and displaying received data in HTML format. Additionally, there are HTML forms for user input and displaying results based on conditions like student marks.

Uploaded by

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

Untitled Document

The document contains Java programs for creating and managing employee and student databases, including creating tables, inserting records, and querying data based on conditions. It also includes servlet code for handling form submissions and displaying received data in HTML format. Additionally, there are HTML forms for user input and displaying results based on conditions like student marks.

Uploaded by

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

Practical 18

1. Develop a program to create employee table in database having two colum


emp_id” and “emp_name”.
package sanskruti;
import java.sql.*;
class Pracical18
{
public static void main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp", "root",
"");
Statement s = con.createStatement();
s.execute("create table employee ( emp_id varchar(20),emp_name
varchar(20))");
s.execute("insert into employee values('19202C1005','Aidihim')");
s.execute("select * from employee");
ResultSet rs = s.getResultSet();
if (rs != null)
while (rs.next()) {
System.out.println("Employee ID: " + rs.getString(1));
System.out.println("Employee Name: " + rs.getString(2));
}
s.close();
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
2.Develop a program to display the name and roll_no of students from
“student table” having percentage > 70.

package sanskruti;
import java.sql.*;
class Practical18 { // Class names should start with capital letter
public static void main(String args[]) {
try {
// Load the MySQL driver
Class.forName("com.mysql.cj.jdbc.Driver"); // Updated to newer driver name
// Establish connection
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/marks",
"root", "" ); // Make sure to set your actual password here );
Statement s = con.createStatement();
s.execute("CREATE TABLE IF NOT EXISTS student (" +
"stu_id VARCHAR(10)," +
"stu_name VARCHAR(20)," +
"marks INTEGER(3))");
s.execute("INSERT INTO student VALUES('142', 'sanskruti', 74)");
s.execute("INSERT INTO student VALUES('131', 'sans', 69)");
ResultSet rs = s.executeQuery("SELECT * FROM student WHERE marks > 70");
while (rs.next()) {
System.out.println("Student ID: " + rs.getString("stu_id"));
System.out.println("Student Name: " + rs.getString("stu_name"));
System.out.println("Student Marks: " + rs.getInt("marks"));
System.out.println("------------------------");
}
if (rs != null) rs.close();
if (s != null) s.close();
if (con != null) con.close();
} catch (Exception e) {
System.out.println("Error occurred: ");
e.printStackTrace(); // Better error reporting
}
}
}
package databaseconnectivity;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
public class DemoDatabaseConnection {
public static void main(String args[]) {
try {
// Load MySQL JDBC Driver
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver loaded");
String url = "jdbc:mysql://localhost:3306/stu";
String user = "root";
String password = "";
Connection cn = DriverManager.getConnection(url, user, password);
System.out.println("Connection to the database created");
Statement st = cn.createStatement();
String str = "SELECT * FROM student";
ResultSet rs = st.executeQuery(str);
System.out.println("Roll Number \t Name");
while (rs.next()) {
System.out.println(rs.getInt(1) + "\t" + rs.getString(2));
}
rs.close();
st.close();
cn.close();
} catch (SQLException e) {
System.out.println("SQL error: " + e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("Driver not found: " + e.getMessage());
}
}
}
Practical 22
Java code

import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/MyForm")
public class Helloservlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String em = request.getParameter("email");
String un = request.getParameter("uname");
String pa = request.getParameter("pass");
String ge = request.getParameter("gender");
String co = request.getParameter("Course");
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.write("<h2>Following data received successfully:</h2><br>");
pw.write("<h3>Email: " + em + "</h3>");
pw.write("<h3>Username: " + un + "</h3>");
pw.write("<h3>Password: " + pa + "</h3>");
pw.write("<h3>Gender: " + ge + "</h3>");
pw.write("<h3>Course: " + co + "</h3>");
pw.close(); // Close the PrintWriter
}
}

Htmlcode:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>button demo</title>
</head>
<body>
Welcome
<br>
<br>
<form method= "get" action="Helloservlet">
Email : <input type="text" name="email"> <br>
User Name: <input type="text" name="uname"> <br>
Password: <input type="password" name="pass"> <br>
Gender : <input type="radio" name="gender" value="male" checked> Male <input
type="radio" name="gender" value="female">
Female <br>
Course: <select name="Course">
<option name ="Course">Advanced Java </option>
<option name ="Course">Javascript</option>
</select>
<input type="submit" value="Submit"> <input type="reset">
</form>
</body>
</html>

Following data received successfully

Email: [email protected]
Username:sanskruti
Password:12345
Gender:female
Course:advance java
2.
Javacode:

import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class Myform extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException
{
PrintWriter pw = res.getWriter();
res.setContentType("text/html");
String name = req.getParameter("name");
String marks = req.getParameter("marks");
int m = Integer.parseInt(marks); if(m>=80)
{
pw.println("Passed");
}
else{ pw.println("Failed");
}
}
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,
ServletException
{ doGet(req,res);
}
}

Htmlcode:
!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>servletexample</title>
</head>
<body>
<form method="get" action="MyForm">
Enter the name:<input type ="text" name ="name">
Enter the marks:<input type ="text" name ="marks">
<input type="submit" value="Enter">
</form>
</body>
</html>
Output;-

passed

You might also like