You are on page 1of 19

AJAVA (3360701) 17

Experiment:- 7
Aim:- Develop a database application that uses any JDBC driver
Code:
package prac7;
import java.sql.*;
public class prac7
{
public static void main(String args[])
{
ResultSet rs;
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/ajava","root","");
Statement st = con.createStatement();
String sql = "CREATE TABLE STUD " +
"(id INTEGER not NULL, " +
" name VARCHAR(255))";
st.executeUpdate(sql);

System.out.println("table Created Succesfully");


con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 18

Experiment:- 8
Aim:- Develop a Graphical User Interface that performs the following SQL operations: a) Insert b)
Delete c)Update.
Code:
package prac8;

import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
import java.lang.*;

public class prac8 extends JFrame {


JLabel lblName,lblId;
JTextField tfName,tfId;
JButton btnInsert,btnDelete,btnUpdate;
public static int id;
prac8(){
super("prac8");
lblId=new JLabel("Id: ");
lblName=new JLabel("Name: ");
lblId.setBounds(20, 20, 100, 25);
lblName.setBounds(20, 50, 100, 25);

tfId=new JTextField(20);
tfName=new JTextField(20);
tfId.setBounds(130, 20, 150, 25);
tfName.setBounds(130, 50, 150, 25);

btnInsert=new JButton("INSERT");
btnDelete=new JButton("DELETE");
btnUpdate=new JButton("UPDATE");
btnDelete.setBounds(260, 120, 100, 30);
btnInsert.setBounds(20, 120, 100, 30);
btnUpdate.setBounds(140, 120, 100, 30);

setLayout(null);

add(lblId);
add(tfId);
add(lblName);
add(tfName);
add(btnInsert);
add(btnUpdate);
add(btnDelete);
setSize(600,400);
setVisible(true);

btnInsert.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
id = Integer.parseInt(tfId.getText());

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 19

try{
performQuery("Insert into stud(id,name) values('"+id+"','"+tfName.getText()+"')");
}
catch(Exception e1){}
}
});
btnDelete.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
id = Integer.parseInt(tfId.getText());
try{
performQuery("delete from stud where id='"+id+"'");
}
catch(Exception e1){}
}
});
btnUpdate.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
id = Integer.parseInt(tfId.getText());
performQuery("update stud set name='"+tfName.getText()+"' where id='"+id+"'");
}
});
}

public void performQuery(String query){

try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/ajava", "root", "");
Statement st=con.createStatement();
st.executeUpdate(query);
JOptionPane.showMessageDialog(null,"Query Executed :)");
}catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Query Not Executed :(");
}
}
public static void main(String[] args) {
new prac8();
}
}

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 20

Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 21

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 22

Experiment:- 9
Aim:- Develop a program to present a set of choice for user to select a product and display the price of
product.
Code:
package prac9;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class prac9 extends JFrame implements ItemListener


{
JComboBox comboProductList;
JLabel lblID,lblIDValue,lblPrice,lblPriceValue;
public String names[]={"Choose..","Desktop","Laptop","KeyBoard","Mouse"};

DataDB db=new DataDB();

JPanel p1,p2;

prac9()
{
setLayout(new FlowLayout());
p1=new JPanel();
comboProductList=new JComboBox(names);
p1.add(comboProductList);

p2=new JPanel();
lblID=new JLabel("ID: ");
lblIDValue=new JLabel("");
lblPrice=new JLabel(" Price: ");
lblPriceValue=new JLabel("");

p2.add(lblID);
p2.add(lblIDValue);
p2.add(lblPrice);
p2.add(lblPriceValue);

comboProductList.addItemListener(this);

add(p1);
add(p2);

setSize(500,230);
setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}
public void itemStateChanged(ItemEvent ie)
{

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 23

String Name=(String)comboProductList.getItemAt(comboProductList.getSelectedIndex());

try{
db.getData(Name);
lblIDValue.setText(String.valueOf(DataDB.id));
lblPriceValue.setText(String.valueOf(DataDB.p_price));
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String[] args) {
new prac9();
}
}
Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 24

Experiment:- 10
Aim:- Develop a simple servlet program which maintains a counter for the number of times it has been
accessed since its loading, initialize the counter using deployment descriptor.
prac10.java:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class prac10 extends HttpServlet


{
int c;
public void init()
{
ServletConfig s=getServletConfig();
c=Integer.parseInt(s.getInitParameter("counter"));
}
public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
{
c++;
PrintWriter out = res.getWriter();
out.println("Total Hit: " + c);
}
}
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>pagecounter</servlet-name>
<servlet-class>prac10</servlet-class>

<init-param>
<param-name>counter</param-name>
<param-value>0</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>pagecounter</servlet-name>
<url-pattern>/counter</url-pattern>
</servlet-mapping>
</web-app>

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 25

Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 26

Experiment:- 11
Aim:- Create a web form which processes servlet and demonstrates use of cookies and sessions.
Index.html:
<html>
<head>
<title> Login Page </title>
</head>

<body>
<form action="loginservlet" method="post">
<table>
<tr>
<td>User Name: </td>
<td> <input type="text" name="name"> </td>
</tr>
<tr>
<td>Password: </td>
<td> <input type="password" name="pass"> </td>
</tr>
<tr>
<td> </td>
<td> <input type="submit" name="submit" value="SUBMIT"> </td>
</tr>
</form>
</body>
</html>
Loginservlet.java:
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class loginservlet extends HttpServlet
{
protected void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String name=req.getParameter("name");
String password=req.getParameter("pass");

if(name.equals("admin") && password.equals("admin")) {


HttpSession session=req.getSession();
session.setAttribute("Name", name);
out.println("<br/><h1>Welcome :"+ name + "</h1> "); out.println("<br/> <a href=profile> Click here to Go Profile
</a>");
}
else{
out.println("<h1>You Have entered Wrong Password </h1>"); out.println("<br/> <a href=index.html> Click here </a>
to Login"); }
}

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 27

Profile.java:
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class profile extends HttpServlet
{
protected void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

res.setContentType("text/html");
PrintWriter out=res.getWriter();

HttpSession session=req.getSession(false);
if(session!=null) {
String name=(String) session.getAttribute("Name"); out.println("Welcome," + name);
out.println("<h1>This is Session Program. </h1> ");
out.println("<a href=logoutservlet> Click here to Logout </a>");
}
else {
out.println("<br> <h1> Plz Login First</h1>");
out.println("<br/> <a href=index.html> Click here </a> to Login");
}
}
}
Logoutservlet.java:
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class logoutServlet extends HttpServlet
{
public void doGet (HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();

HttpSession session=req.getSession();
session.invalidate();

out.println("<br> <h1>You have successfully Logout </h1>"); out.println("<br/> <a href=index.html> Click here </a>
to Login"); }

}
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>loginservlet</servlet-name> <servlet-class>loginservlet</servlet-class>
</servlet>

<servlet>
<servlet-name>logoutservlet</servlet-name> <servlet-class>logoutServlet</servlet-class>
</servlet>

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 28

<servlet>
<servlet-name>profile</servlet-name> <servlet-class>profile</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>profile</servlet-name> <url-pattern>/profile</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>logoutservlet</servlet-name> <url-pattern>/logoutservlet</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>loginservlet</servlet-name> <url-pattern>/loginservlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 29

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 30

Experiment:- 12
Aim:- Develop a simple JSP program for user registration and then control will be transfer it into second
page.
Index.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="second.jsp" method="post">
Enter your UserName: <input type="text" name="name"><br>
Enter your Password: <input type="password" name="pas"><br>
Enter your email: <input type="email" name="email"><br>

<input type="submit">
</form>
</body>
</html>
Second.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String name=request.getParameter("name");
String password=request.getParameter("pas");
String email=request.getParameter("email");

out.println("You Entered Name : "+name+"<br>");


out.println("You Entered pass : "+password+"<br>");
out.println("You Entered email : "+email+"<br>");
%>
</body>
</html>

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 31

Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 32

Experiment:- 13
Aim:- Develop a simple JSP program for user login form with static and dynamic database.
Index.html:
<html>
<body>
<form action="static.jsp" method="get">
UserName: <input type="text" name="uname"><br><br>
Password: <input type="password" name="pass"><br><br>
<input type="submit" value="Static Login">
<input type="submit" value="Dynamic Login" formaction="Dynamic.jsp">
</form>
</body>
</html>
Static.jsp:
<html>
<body>
<%
String s1="admin";
String s2="admin";
String uname=request.getParameter("uname");
String pass=request.getParameter("pass");
if(uname.equals(s1) && pass.equals(s2))
{
out.println("user Login Successfully");
}
else
{
out.println("Invalid user");
}
%>
</body>
</html>
Dynamic.jsp:
<%@page import="java.io.*"%>
<%@page import="java.util.*" %>
<%@page import="java.sql.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Practical 13</title>
</head>
<body>
<%
String uname=request.getParameter("uname");
String pass=request.getParameter("pass");

Class.forName("com.mysql.cj.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/ajava","root","");

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 33

PreparedStatement ps=con.prepareStatement("select * from user where uname=? and pas=?");


ps.setString(1, uname);
ps.setString(2, pass);

ResultSet rs=ps.executeQuery();

if(rs.next())
{
out.println("<h1>Welcome :"+uname+"</h1>");
}
else
{
out.println("<h1>Not Done</h1>");
}
%>
</body>
</html>
Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 34

Experiment:- 14
Aim:- Develop a JSP program to display the grade of a student by accepting the marks of five subjects.
Index.html:
<!DOCTYPE html>
<html>
<head>
<title>Practical 14</title>
</head>
<body>
<h1>Student Marks</h1>
<form action="student.jsp" method="GET">
AJAVA:<input type="number" name="ajava"><br>
PPD:<input type="number" name="ppd"><br>
NMA:<input type="number" name="nma"><br>
MCA:<input type="number" name="mca"><br>
<input type="submit" value="Calculate Grade">
</form>
</body>
</html>
Student.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Practical 14</title>
</head>
<body>
<%
int ajava=Integer.parseInt(request.getParameter("ajava"));
int ppd=Integer.parseInt(request.getParameter("ppd"));
int nma=Integer.parseInt(request.getParameter("nma"));
int mca=Integer.parseInt(request.getParameter("mca"));

int c=ajava+nma+mca+ppd;
double per=c/5;

if(per>60 && per<100)


{
out.println("<h1>Grade A");
}
else if(per>50 && per<60)
{
out.println("<h1>Grade B");
}
else if(per>35 && per<50)
{
out.println("<h1>Grade C");
}
else
{

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045


AJAVA (3360701) 35

out.println("<h1>Fail");
}
%>
</body>
</html>
Output:

TAPI DIPLOMA ENGINEERING COLLEGE 196470307045

You might also like