You are on page 1of 14

Experiment 3

20BDS0251

1.Add an authentication table to any of yours Database, then Design a simple Java Front end
application that authenticate the user who want to access the first name of the given username
once it was validated against the password. If he is authenticated, let him to access the Database
using various DML statements.

2. Create the following database relations and insert the records

a. Develop a Java Program to display the activities performed by each student along
with his name.

import jakarta.jms.Connection;

import jakarta.resource.cci.ResultSet;

import java.beans.Statement;

public class NewClass


{

public static void main(String[] args)

try

String myDriver = "org.gjt.mm.mysql.Driver";

String myUrl = "jdbc:mysql://localhost/test";

Class.forName(myDriver);

Connection conn = DriverManager.getConnection(myUrl, "root", "");

String query = "SELECT student,activity1,activity2 FROM students INNER JOIN activities ON


students.id = activities.id;";

Statement st = conn.createStatement();

ResultSet rs = st.executeQuery(query);

while (rs.next())

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

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

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

System.out.format("%s, %s, %s\n", student,activity1,activity2);

st.close();

catch (ClassNotFoundException e)

System.err.println("Got an exception! ");

System.err.println(e.getMessage());

b. Find out the total cost collected from all the students
import jakarta.jms.Connection;

import jakarta.resource.cci.ResultSet;

import java.beans.Statement;

import java.sql.SQLException;

public class NewClass

public static void main(String[] args) throws SQLException

try

String myDriver = "org.gjt.mm.mysql.Driver";

String myUrl = "jdbc:mysql://localhost/test";

Class.forName(myDriver);

Connection conn = DriverManager.getConnection(myUrl, "root", "");

String query = "SELECT cost1+cost2 as cost FROM activities ;";

Statement st = conn.createStatement();

ResultSet rs = st.executeQuery(query);

while (rs.next())

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

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

System.out.format("%s, %s\n",student,cost);

st.close();

catch (ClassNotFoundException e)

System.err.println("Got an exception! ");

System.err.println(e.getMessage());
}

c. List out the name of the students who have registered for swimming activity.

public class NewClass

public static void main(String[] args)

try

String myDriver = "org.gjt.mm.mysql.Driver";

String myUrl = "jdbc:mysql://localhost/test";

Class.forName(myDriver);

Connection conn = DriverManager.getConnection(myUrl, "root", "");

String query = "SELECT student FROM students INNER JOIN activities ON students.id = activities.id
where activities.activity1='swimming' || activity2='swimming';";

Statement st = conn.createStatement();

ResultSet rs = st.executeQuery(query);

while (rs.next())

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

System.out.format("%s\n", student);

st.close();

catch (ClassNotFoundException e)

System.err.println("Got an exception! ");

System.err.println(e.getMessage());
}

d. Add a GST column to the activity table and fill-up with 8% GST for each student.

import java.sql.*;

public class NewClass {

public static void main(String[] args) throws Exception {

Class.forName("org.apache.derby.jdbc.ClientDriver");

Connection con =
DriverManager.getConnection("jdbc:derby://localhost:1527/testDb","name","pass");

Statement stmt = con.createStatement(

ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

String insertEmp1 = "ALTER TABLE activities Add gst Varchar(50)";

String insertEmp2 = "UPDATE activities SET gst = 18% WHERE id = 084";

String insertEmp3 = "UPDATE activities SET gst = 18% WHERE id = 100";

String insertEmp4 = "UPDATE activities SET gst = 18% WHERE id = 182";

String insertEmp5 = "UPDATE activities SET gst = 18% WHERE id = 219";

con.setAutoCommit(false);

stmt.addBatch(insertEmp1);

stmt.addBatch(insertEmp2);

stmt.addBatch(insertEmp3);

stmt.addBatch(insertEmp4);

stmt.addBatch(insertEmp5);

ResultSet rs = stmt.executeQuery("select * from emp");

rs.last();
System.out.println("rows before batch execution= "+ rs.getRow());

stmt.executeBatch();

con.commit();

System.out.println("Batch executed");

rs = stmt.executeQuery("select * from emp");

rs.last();

System.out.println("rows after batch execution = "+ rs.getRow());

e. Delete the entry from the given relations where the student has not registered for
any second activity

public class NewClass

public static void main(String[] args)

try

String myDriver = "org.gjt.mm.mysql.Driver";

String myUrl = "jdbc:mysql://localhost/test";

Class.forName(myDriver);

Connection conn = DriverManager.getConnection(myUrl, "root", "");

String query = "delete FROM students INNER JOIN activities ON students.id = activities.id where
activities.activity2=NULL;";

Statement st = conn.createStatement();

ResultSet rs = st.executeQuery(query);

while (rs.next())

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

System.out.format("%s\n", student);
}

st.close();

catch (ClassNotFoundException e)

System.err.println("Got an exception! ");

System.err.println(e.getMessage());

3. Write a Hello-world Java Servlet - "HelloServlet.java" which will display helloworld


message along with the following details
a.Dispaly the URL

b.Display the name of the protocol which invokes the servlet

c.display the path information of the servlet

d.Display the remote address of the request sent to the servlet.

import java.io.*;

import jakarta.servlet.ServletException;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/UrlInformationServlet")

public class NewServlet extends HttpServlet {

@Override

protected void doGet(

HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

@Override

protected void doPost(

HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

String urlInfo = request.getRequestURL().toString();

String schemeInfo = request.getScheme();

int portNumber = request.getServerPort();

String servletPath = request.getServletPath();

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<h1>Hello World</h1>");

out.print("Url of the request : " +urlInfo + "<br/>");

out.print("Scheme of the request : " + schemeInfo + "<br/>");

out.print("Port Number of the request : "+ portNumber + "<br/>");

out.print("Servlet Path of the request : " +servletPath + "<br/>");

}
4. Create a login form using HTML Form and authenticate the given username and password
against the record of a database table located in the server.

HTML

<!DOCTYPE html>

<html>

<head>

<title>TODO supply a title</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<form action="NewServlet" method="post">

Username:<input type="text" name="username" />

Password:<input type="password" name="password" />


<input type="submit" name="submit" />

</form>

</body>

</html>

Servlet

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;

@WebServlet(urlPatterns = {"/NewServlet"})

public class NewServlet extends HttpServlet

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

try (PrintWriter pw = response.getWriter()) {

response.setContentType("text/html");

String user=request.getParameter("username");

String pass=request.getParameter("password");

if(user.equals("manali") && pass.equals("manali"))

pw.println("Login Success...!");

else

pw.println("Login Failed...!");

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);
}

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

@Override

public String getServletInfo() {

return "Short description";

}}

5. Create the following form in HTML Form and Store the content of the HTML form to a
database located in a web server

Html

<!DOCTYPE html>

<html>
<head>

<title>TODO supply a title</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<form action="NewServlet" method="post">

Username:<input type="text" name="username" /></br></br>

Password:<input type="password" name="password" /></br></br>

Gender:</br></br>

Male<input type="radio" name="gender" value="Male">

Female<input type="radio" name="gender" value="Female"></br></br>

Age:<input type="text" name="age" placeholder:"< 1 years old"/></br></br>

Languages:</br></br>

Java<input type="radio" name="language" value="Java">

C/C++<input type="radio" name="language" value="C/C++">

C#<input type="radio" name="language" value="C#"></br></br>

<input type="submit" name="submit" />

</form>

</body>

</html>

Servlet

import java.io.*;

import java.sql.*;

import jakarta.servlet.*;

import jakarta.servlet.http.*;

public class NewServlet extends HttpServlet{

@Override

public void service(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

try{
String username = request.getParameter("username");

String password = request.getParameter("password");

String gender = request.getParameter("gender");

String age = request.getParameter("age");

String language = request.getParameter("language");

String instruction = request.getParameter("instruction");

out.println(username);

out.println(password);

out.println(gender);

out.println(age);

out.println(language);

out.println(instruction);

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection
con=DriverManager.getConection("jdbc:oracle:thin:@localhost:1521:xe","jaiprakash","admin123");

PreparedStatement pst = con.prepareStatement("insert into emp_info values(?,?)");

pst.setString(1,username);

pst.setString(2,password);

pst.setString(3,gender);

pst.setString(4,age);

pst.setString(5,language);

pst.setString(6,instruction);

int i = pst.executeUpdate();

if(i!=0){

out.println("<br>Record has been inserted");

else{

out.println("failed to insert the data");

catch (ClassNotFoundException | SQLException e){

out.println(e);

}
6. Write a program to demonstrate the knowledge of students in JSP programming.

Eg., Assume two cookies are created whenever a VIT student visits the VIT webpage-one for his/her
name and the other for his campus. For subsequent visits, he/she should be greeted with the
message similar to the one below

“Hi Ajay from Chennai Campus!!”.

Write a servlet program to do the needful.

7. Solve the Question 4 using JSP .If username is not exist allow the user to register for it.

8. Create a simple Web site to provide information about your domain using JSP.

You might also like