Professional Documents
Culture Documents
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);
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.*;
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());
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+"'");
}
});
}
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();
}
}
Output:
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.*;
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)
{
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:
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;
<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>
Output:
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");
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>
<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:
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");
Output:
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","");
ResultSet rs=ps.executeQuery();
if(rs.next())
{
out.println("<h1>Welcome :"+uname+"</h1>");
}
else
{
out.println("<h1>Not Done</h1>");
}
%>
</body>
</html>
Output:
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;
out.println("<h1>Fail");
}
%>
</body>
</html>
Output: