You are on page 1of 40

NAME:SIDDESH KHARADE ROLL NO.

:92

PRACTICAL NO.1
Aim: Develop the presentation layer of Library Management software application with
suitable menus.

SOURCE CODE:

import java.awt.*;
import javax.swing.*;
public class MenuTest extends JFrame{
JMenu menu1,menu2,menu3,menu4,menu5;
JMenuBar menubar1;
JMenuItem menuitem1,menuitem2,menuitem3,menuitem4,menuitem5,menuitem6;
JMenuItem menuitem7,menuitem8,menuitem9,menuitem10,menuitem11;
JMenuItem menuitem12,menuitem13,menuitem14,menuitem15;
public MenuTest(){
menubar1=new JMenuBar();
setJMenuBar(menubar1);
menu1=new JMenu("Master File");
menu2=new JMenu("Transaction File");
menu3=new JMenu("Search File");
menu4=new JMenu("Report Screen");
menu5=new JMenu("Help");
menuitem1=new JMenuItem("Book details");
menuitem2=new JMenuItem("Student master");
menuitem3=new JMenuItem("Library master");
menuitem4=new JMenuItem("Tearch Master");
menuitem5=new JMenuItem("Fine details");
menuitem6=new JMenuItem("Order Deatails");
menuitem7=new JMenuItem("Student search");

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 1


NAME:SIDDESH KHARADE ROLL NO.:92

menuitem8=new JMenuItem("Tearch search");


menuitem9=new JMenuItem("Book search");
menuitem10=new JMenuItem("Student report");
menuitem11=new JMenuItem("Fine report");
menuitem12=new JMenuItem("Order report");
menuitem13=new JMenuItem("About us");
menuitem14=new JMenuItem("Exit");
menuitem15=new JMenuItem("logout");
menu1.add(menuitem1);menu1.add(menuitem2);
menu1.add(menuitem3);menu1.add(menuitem4);
menu2.add(menuitem5);menu2.add(menuitem6);
menu2.add(menuitem7);menu3.add(menuitem8);
menu3.add(menuitem9);menu4.add(menuitem10);
menu4.add(menuitem11);menu4.add(menuitem12);
menu5.add(menuitem13);menu5.add(menuitem14);
menu5.add(menuitem15);
menubar1.add(menu1);menubar1.add(menu2);
menubar1.add(menu3);menubar1.add(menu4);
menubar1.add(menu5);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800,400);
setVisible(true);
}
public static void main(String[] args) {
new MenuTest();
}
}

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 2


NAME:SIDDESH KHARADE ROLL NO.:92

Output:-

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 3


NAME:SIDDESH KHARADE ROLL NO.:92

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 4


NAME:SIDDESH KHARADE ROLL NO.:92

PRACTICAL NO.2
Aim: Design suitable database for Library Management System.
Source Code:
import java.awt.*;
import javax.swing.*;
public class bookdetails extends JFrame{
JTextField
textfield_id,textfield_name,textfield_author,textfield_price,textfield_bn,textfield_edition;
JLabel label_id,label_name,label_author,label_price,label_bn,label_edition;
JButton button_save,button_retrive,button_Exit;
JTextArea textarea_d;
public bookdetails()
{
setLayout(null);
label_id=new JLabel("Book Id");
label_id.setBounds(10,10,100,30);
textfield_id=new JTextField(20);
textfield_id.setBounds(120,10,200,30);

label_name=new JLabel("Book Name");


label_name.setBounds(10,50,100,30);
textfield_name=new JTextField(20);
textfield_name.setBounds(120,50,200,30);

label_author=new JLabel("Author");
label_author.setBounds(10,110,100,30);
textfield_author=new JTextField(20);
textfield_author.setBounds(120,110,200,30);

label_price=new JLabel("price");

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 5


NAME:SIDDESH KHARADE ROLL NO.:92

label_price.setBounds(10,160,100,30);
textfield_price=new JTextField(20);
textfield_price.setBounds(120,160,200,30);

label_bn=new JLabel("isbn_no");
label_bn.setBounds(10,220,100,30);
textfield_bn=new JTextField(20);
textfield_bn.setBounds(120,220,200,30);

label_edition=new JLabel("Book Edition");


label_edition.setBounds(10,270,100,30);
textfield_edition=new JTextField(20);
textfield_edition.setBounds(120,270,200,30);

button_save=new JButton("Save");
button_save.setBounds(10,400,100,50);
button_retrive=new JButton("Retrive");
button_retrive.setBounds(120,400,100,50);
button_Exit=new JButton("Exit");
button_Exit.setBounds(240,400,100,50);

textarea_d=new JTextArea(5,30);
textarea_d.setBounds(50,320,300,70);
JScrollPane sp1=new JScrollPane(textarea_d);
add(label_id);add(textfield_id);
add(label_name);add(textfield_name);
add(label_author);add(textfield_author);
add(label_price);add(textfield_price);
add(label_bn);add(textfield_bn);
add(label_edition);add(textfield_edition);

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 6


NAME:SIDDESH KHARADE ROLL NO.:92

add(textarea_d);add(button_retrive);add(button_save);add(button_Exit);

setSize(400,500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[])
{
new bookdetails();
}
}
Output:

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 7


NAME:SIDDESH KHARADE ROLL NO.:92

PRACTICAL NO.3
Aim: Develop business logic layer for Library Management System.
CONFIGURE:

Source Code:
package allpractical;

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.*;
public class AllPractical extends JFrame{
JTextField
textfield_id,textfield_name,textfield_author,textfield_price,textfield_bn,textfield_edition;
JLabel label_id,label_name,label_author,label_price,label_bn,label_edition;
JButton button_save,button_retrive,button_Exit;
JTextArea textarea_d;
public AllPractical()
{
setLayout(null);
label_id=new JLabel("Book Id");
label_id.setBounds(10,10,100,30);
textfield_id=new JTextField(20);
textfield_id.setBounds(120,10,200,30);

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 8


NAME:SIDDESH KHARADE ROLL NO.:92

label_name=new JLabel("Book Name");


label_name.setBounds(10,50,100,30);
textfield_name=new JTextField(20);
textfield_name.setBounds(120,50,200,30);

label_author=new JLabel("Author");
label_author.setBounds(10,110,100,30);
textfield_author=new JTextField(20);
textfield_author.setBounds(120,110,200,30);

label_price=new JLabel("price");
label_price.setBounds(10,160,100,30);
textfield_price=new JTextField(20);
textfield_price.setBounds(120,160,200,30);

label_bn=new JLabel("isbn_no");
label_bn.setBounds(10,220,100,30);
textfield_bn=new JTextField(20);
textfield_bn.setBounds(120,220,200,30);

label_edition=new JLabel("Book Edition");


label_edition.setBounds(10,270,100,30);
textfield_edition=new JTextField(20);
textfield_edition.setBounds(120,270,200,30);

button_save=new JButton("Save");
button_save.setBounds(10,400,100,50);
button_retrive=new JButton("Retrive");
button_retrive.setBounds(120,400,100,50);

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 9


NAME:SIDDESH KHARADE ROLL NO.:92

button_Exit=new JButton("Exit");
button_Exit.setBounds(240,400,100,50);

textarea_d=new JTextArea(5,30);
textarea_d.setBounds(50,320,300,70);
JScrollPane sp1=new JScrollPane(textarea_d);

add(label_id);add(textfield_id);
add(label_name);add(textfield_name);
add(label_author);add(textfield_author);
add(label_price);add(textfield_price);
add(label_bn);add(textfield_bn);
add(label_edition);add(textfield_edition);
add(textarea_d);add(button_retrive);add(button_save);add(button_Exit);
button_save.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/adv_java";
String username="root",pass="";
Connection conn=DriverManager.getConnection(url,username,pass);
String sql="insert into Library(bid,bname,author,bprice,bn_no,bedition)
values(?,?,?,?,?,?)";
PreparedStatement pr=conn.prepareStatement(sql);
int id=Integer.parseInt(textfield_id.getText());
String name,author,price,bn_no,edition;
name=textfield_name.getText();
author=textfield_author.getText();
price=textfield_price.getText();

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 10


NAME:SIDDESH KHARADE ROLL NO.:92

bn_no=textfield_bn.getText();
edition=textfield_edition.getText();
pr.setInt(1,id);
pr.setString(2,name);
pr.setString(3,author);
pr.setString(4,price);
pr.setString(5,bn_no);
pr.setString(6,edition);
pr.executeUpdate();
pr.close();
conn.close();
}
catch(Exception ex){
System.out.println("ERROR:"+ex.getMessage());
}
}
});

button_Exit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
button_retrive.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/adv_java";

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 11


NAME:SIDDESH KHARADE ROLL NO.:92

String username="root",pass="";
Connection conn=DriverManager.getConnection(url,username,pass);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from Library");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
conn.close();
}
catch(Exception ex){
System.out.println("ERROR:"+ex.getMessage());
}
}
});

setSize(400,500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[])
{
new AllPractical();
}
}
Output:-

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 12


NAME:SIDDESH KHARADE ROLL NO.:92

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 13


NAME:SIDDESH KHARADE ROLL NO.:92

PRACTICAL NO.4
Aim: Develop Java application to store image in a database as well as retrieve image from
database.
Source Code:
 imageDemo.java
import java.sql.*;
import imagedb1.DBClass;
import java.awt.FileDialog;
import java.io.*;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class imageDemo extends javax.swing.JFrame {


public imageDemo() {
initComponents();
}

String path=null;
public void save()
{
try {
FileInputStream fis=null;
File img=null;
img=new File(path);
fis=new FileInputStream(img);
if(new DBClass().add(img, fis))
{

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 14


NAME:SIDDESH KHARADE ROLL NO.:92

JOptionPane.showMessageDialog(null,"Image Saved successfully");


jLabel2.setIcon(null);
}
else
{
JOptionPane.showMessageDialog(null,"Image Not saved");
}

}catch(Exception ex){}
}

public void retrieve()


{
String id=new DBClass().retrieve();
if(! id.equals("-1"))
{
JOptionPane.showMessageDialog(null,"Image Retrieved");
jLabel1.setIcon( new ImageIcon(id+".sjpg"));
}
else
{
JOptionPane.showMessageDialog(null,"Not Retrieved");
}

@SuppressWarnings("unchecked")

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 15


NAME:SIDDESH KHARADE ROLL NO.:92

// <editor-fold defaultstate="collapsed" desc="Generated Code">


private void initComponents() {

jLabel1 = new javax.swing.JLabel();


jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("db_label");

jLabel2.setText("select_label");

jButton1.setText("Browse");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Save");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 16


NAME:SIDDESH KHARADE ROLL NO.:92

}
});

jButton3.setText("Retrieve");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

jButton4.setText("Clear");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 136,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 130,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27))
.addGroup(layout.createSequentialGroup()

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 17


NAME:SIDDESH KHARADE ROLL NO.:92

.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 67,


javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28)
.addComponent(jButton3)
.addGap(64, 64, 64)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 83,
Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(84, 84, 84))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 120,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 120,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 18


NAME:SIDDESH KHARADE ROLL NO.:92

.addComponent(jButton3)
.addComponent(jButton4))
.addContainerGap(71, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


FileDialog fd=new FileDialog(this);
fd.show();
path=fd.getDirectory()+fd.getFile();
jLabel2.setIcon(new ImageIcon(path));
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {


this.save();
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {


this.retrieve();
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 19


NAME:SIDDESH KHARADE ROLL NO.:92

/* Set the Nimbus look and feel */


//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and
feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(imageDemo.class.getName()).log(java.util.logging.Level
.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(imageDemo.class.getName()).log(java.util.logging.Level
.SEVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(imageDemo.class.getName()).log(java.util.logging.Level
.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(imageDemo.class.getName()).log(java.util.logging.Level
.SEVERE, null, ex);
}

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 20


NAME:SIDDESH KHARADE ROLL NO.:92

//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new imageDemo().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
// End of variables declaration
}

 Design:

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 21


NAME:SIDDESH KHARADE ROLL NO.:92

 DBClass.java
package imagedb1;

import java.sql.*;

import java.io.*;

public class DBClass

String username="root";

String url="jdbc:mysql://localhost:3306/db2";

String pass="12345";

public Boolean add(File img,FileInputStream fis)

try{

String sql="insert into imagedb(Image) values(?)";

Connection con= DriverManager.getConnection(url,username,pass);

PreparedStatement ps=con.prepareStatement(sql);

ps.setBinaryStream(1, fis,(int)img.length());

ps.executeUpdate();

return true;

}catch(Exception ex){

return false;

public String retrieve()

try{

FileOutputStream fos=null;

String sql="select * from imagedb";

Connection con=DriverManager.getConnection(url,username,pass);

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 22


NAME:SIDDESH KHARADE ROLL NO.:92

PreparedStatement ps=con.prepareStatement(sql);

ResultSet rs=ps.executeQuery();

rs.last();

Blob b=rs.getBlob("Image");

String id=rs.getString("ID");

fos=new FileOutputStream(id+".jpg");

int len=(int)b.length();

byte[]buf=b.getBytes(1, len);

fos.write(buf,0,len);

return id;

}catch(Exception ex){}

return "-1";

Output:-

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 23


NAME:SIDDESH KHARADE ROLL NO.:92

PRACTICAL NO.5
Aim: Write a Java application to demonstrate servlet life cycle.
Source Code:
 helloServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class helloServlet extends HttpServlet


{

protected void doGet(HttpServletRequest request,HttpServletResponse response)


throws ServletException, IOException
{
PrintWriter out = response.getWriter();
try {
String a,b,c;
String url="jdbc:mysql://localhost:3306/student";
String username="root";
String pass="12345";
a=request.getParameter("rollno");
b=request.getParameter("sname");
c=request.getParameter("STD");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(url,username,pass);
PreparedStatement p=con.prepareStatement("insert into student values(?,?,?)");
p.setString(1,a);
p.setString(2,b);
p.setString(3,c);
p.executeUpdate();
ResultSet rs=p.executeQuery("Select * from student");
out.println("<table border=1
width=50%><tr><th>Rollno</th><th>Sname</th><th>STD</th></tr>");
while(rs.next())
{
out.println("<tr>");
out.println("<td>"+rs.getString(1)+"</td>");
out.println("<td>"+rs.getString(2)+"</td>");
out.println("<td>"+rs.getString(3)+"<td>");
out.println("</tr>");

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 24


NAME:SIDDESH KHARADE ROLL NO.:92

out.println("</table>");
} catch(Exception ex){

out.println("ggg="+ex.getMessage());
}
}
}

Web.xml
<body>
<form action="helloServlet">
Enter Roll No:<input type="text" name="rollno"><br>
Enter Sname:<input type="text" name="sname"><br>
Enter STD:<input type="text" name="STD"><br>
<input type="submit" name="btn" value="SAVE"><br>
</form>
</body>
</html>

Output:-

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 25


NAME:SIDDESH KHARADE ROLL NO.:92

PRACTICAL NO.6
Aim: Design database for student administration. Develop servlet(s) to perform CRUD
operations.
Source Code:
<%@page contentType="text/html" import="java.sql.*" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>CRUD Operation</title>
</head>
<body>
<form action="CRUDOperation.jsp">
ROLL NO:<input type="text" name="roll"><br>
NAME:<input type="text" name="name"><br>
STD:<input type="text" name="std"><br>
<input type="submit" name="btn1" value="CREATE">
<input type="submit" name="btn2" value="READ"><br><br>
<input type="submit" name="btn3" value="UPDATE">
<input type="submit" name="btn4" value="DELETE"><br>

</form>
<%
int a;
String b,c,id;
String url="jdbc:mysql://localhost:3306/adv_java";
String username="root";
String pass="";
try{

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 26


NAME:SIDDESH KHARADE ROLL NO.:92

if(request.getParameter("btn1").equals("CREATE")){
a=Integer.parseInt(request.getParameter("roll"));
b=request.getParameter("name");
c=request.getParameter("std");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(url, username, pass);
PreparedStatement p=con.prepareStatement("insert into student
values(?,?,?)");
p.setInt(1,a);
p.setString(2,b);
p.setString(3,c);
p.executeUpdate();
}
}catch(Exception ex){
out.println(ex.getMessage());
}
try{
if(request.getParameter("btn2").equals("READ")){
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(url, username, pass);
Statement p=con.createStatement();
ResultSet rs=p.executeQuery("select * from student");
out.println("<table border=1 width='60%'><tr><th>Roll
No</th><th>Name</th><th>STD</th></tr>");
while(rs.next()){
out.println("<tr>");
out.println("<td>"+rs.getInt(1)+"</td>");
out.println("<td>"+rs.getString(2)+"</td>");
out.println("<td>"+rs.getString(3)+"</td>");
out.println("</tr>");

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 27


NAME:SIDDESH KHARADE ROLL NO.:92

}
out.println("</table>");
}

}catch(Exception ex){
out.println(ex.getMessage());
}
try{
if(request.getParameter("btn3").equals("UPDATE")){
a=Integer.parseInt(request.getParameter("roll"));
b=request.getParameter("name");
c=request.getParameter("std");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(url, username, pass);
PreparedStatement p=con.prepareStatement("update student set
rollno=?,sname=?,std=? where rollno=?");
p.setInt(1,a);
p.setString(2,b);
p.setString(3,c);
p.setInt(4,a);
p.executeUpdate();
}
}catch(Exception ex){
out.println(ex.getMessage());
}

try{
if(request.getParameter("btn4").equals("DELETE")){
id=request.getParameter("roll");
Class.forName("com.mysql.jdbc.Driver");

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 28


NAME:SIDDESH KHARADE ROLL NO.:92

Connection con=DriverManager.getConnection(url, username, pass);


Statement st=con.createStatement();
st.executeUpdate("delete from student where rollno='"+id+"'");
}
}catch(Exception ex){
out.println(ex.getMessage());
}
%>
</body>
</html>
Output:-
After Run:-

After Create:

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 29


NAME:SIDDESH KHARADE ROLL NO.:92

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 30


NAME:SIDDESH KHARADE ROLL NO.:92

PRACTICAL NO.7
Aim: Create Employees table in EMP database. Perform select, insert, update, and delete
operations on Employee table using JSP.
Source Code:
<%@page contentType="text/html" import="java.sql.*" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<form action="details.jsp">

Enter EmpNo:<input type="text" name="eno"><br>

Enter Ename:<input type="text" name="ename"><br>

Enter Salary:<input type="text" name="sal"><br>

<input type="submit" name="btn1" value="SAVE">

<input type="submit" name="btn2" value="DELETE"><br>

</form>

<%

int a;

String b,c,id;

String url="jdbc:mysql://localhost:3306/adv_java";

String username="root";

String pass="";

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 31


NAME:SIDDESH KHARADE ROLL NO.:92

try{

if(request.getParameter("btn1").equals("SAVE")){

a=Integer.parseInt(request.getParameter("eno"));

b=request.getParameter("ename");

c=request.getParameter("sal");

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection(url, username, pass);

PreparedStatement p=con.prepareStatement("insert into emp values(?,?,?)");

p.setInt(1,a);

p.setString(2,b);

p.setString(3,c);

p.executeUpdate();

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

out.println("<table border=1
width='60%'><tr><th>Empno</th><th>Ename</th><th>Salary</th></tr>");

while(rs.next()){

out.println("<tr>");

out.println("<td>"+rs.getInt(1)+"</td>");

out.println("<td>"+rs.getString(2)+"</td>");

out.println("<td>"+rs.getString(3)+"</td>");

out.println("</tr>");

out.println("</table>");

}catch(Exception ex){

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 32


NAME:SIDDESH KHARADE ROLL NO.:92

out.println(ex.getMessage());

try{

if(request.getParameter("btn2").equals("DELETE")){

id=request.getParameter("eno");

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection(url, username, pass);

Statement st=con.createStatement();

st.executeUpdate("delete from emp where id='"+id+"'");

}catch(Exception ex){

out.println(ex.getMessage());

%>

</body>

</html>

Output:-

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 33


NAME:SIDDESH KHARADE ROLL NO.:92

PRACTICAL NO.8
Aim: Write a Student class with three properties. The useBean action declares a JavaBean
for use in a JSP. Write Java application to access JavaBeans Properties.
Source Code:

 HTML Code:

<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>

<h1>Student database</h1>

<form action="studata.jsp">

<table border="1">

<tr>

<td>RollNo:</td>

<td><input type="text" name="rollno"></td>

</tr>

<tr>

<td>SName</td>

<td><input type="text" name="sname"></td>

</tr>

<tr>

<td>Mobile:</td>

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 34


NAME:SIDDESH KHARADE ROLL NO.:92

<td><input type="text" name="mobile"></td>

</tr>

<tr>

<td><input type="submit" value="Submit"></td>

<td><input type="reset" value="Clear"></td>

</tr>

</table>

</form>

</body>

</html>

JSP Code:

<%@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>

<h1>Student Details</h1>

<hr color="black" size="5">

<jsp:useBean id="stud" scope="session" class="studBean.student">

</jsp:useBean>

<jsp:setProperty name="stud" property="*"></jsp:setProperty>

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 35


NAME:SIDDESH KHARADE ROLL NO.:92

<jsp:getProperty name="stud" property="rollno"/>

<br>

Sname:<jsp:getProperty name="stud" property="sname"/>

<br>

Mobile<jsp:getProperty name="stud" property="mobile"/>

<br>

<hr>

</body>

</html>

 Java Code

package studBean;

public class student{

int rollno;

String sname,mobile;S

public int getRollno(){

return rollno;

public void setRollno(int n) {

this.rollno=n;

public String getSname(){

return sname;

public void setSname(String str){

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 36


NAME:SIDDESH KHARADE ROLL NO.:92

this.sname=str;

public String getMobile(){

return mobile;

public void setMobile(String no) {

this.mobile=no;

Outputs:-

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 37


NAME:SIDDESH KHARADE ROLL NO.:92

PRACTICAL NO.9
Aim: Write Java application to encoding and decoding JSON in Java.
CONFIGURE:

Source Code:

 Encode
package allpractical;
import org.json.simple.JSONObject;
import java.io.StringWriter;
import java.io.*;
public class EncoderJson {
public static void main(String a[])throws IOException{
JSONObject js=new JSONObject();
js.put("Roll No.",new Integer(21));
js.put("Name", "Ashish");
js.put("STD","SYCS");
js.put("Mark",new Double(87.45));
js.put("pass",new Boolean(true));
StringWriter st=new StringWriter();
js.writeJSONString(st);
String jt=js.toString();
System.out.println(jt);
}

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 38


NAME:SIDDESH KHARADE ROLL NO.:92

Output:-

 B. Decode
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;

public class JsonDecodeDemo {


public static void main(String [] args){
JSONParser parser=new JSONParser();
String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,:{\"6\":7}]}}}}]";

try{
Object obj=parser.parse(s);
JSONArray array=(JSONArray)obj;

System.out.println("The 2nd element of array");


System.out.println(array.get(1));
System.out.println();

JSONObject obj2=(JSONObject)array.get(1);
System.out.println("Field \"1\"");

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 39


NAME:SIDDESH KHARADE ROLL NO.:92

System.out.println(obj2.get(1));

s="{}";
obj=parser.parse(s);
System.out.println(obj);

s="[5,]";
obj=parser.parse(s);
System.out.println(obj);

s="[5,2]";
obj=parser.parse(s);
System.out.println(obj);

}catch (ParseException pe){


System.out.println("position: "+pe.getPosition());
System.out.println(pe);
}
}
}

Output:-

ADVANCED JAVA S.Y.B.Sc.(COMPUTER SCIENCE)SEM-IV pg. 40

You might also like