Anda di halaman 1dari 40

Advance java

Prac 1 presentation library

package library;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class LibrarianLogin extends JFrame {

LibrarianLogin frame;

JPanel contentPane;

JTextField textField;

JPasswordField passwordField;

public LibrarianLogin() {

setLayout(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel lblAdminLoginForm = new JLabel("Librarian Login Form");


lblAdminLoginForm.setBounds(100,0,500,100);

lblAdminLoginForm.setForeground(Color.GRAY);

lblAdminLoginForm.setFont(new Font("Arial", Font.PLAIN, 28));

JLabel lblEnterName = new JLabel("Enter Name:");

lblEnterName.setBounds(10,80,500,100);

lblEnterName.setFont(new Font("Arial", Font.PLAIN, 18));

JLabel lblEnterPassword = new JLabel("Enter Password:");

lblEnterPassword.setBounds(10,150,500,100);

lblEnterPassword.setFont(new Font("Arial", Font.PLAIN, 18));

textField = new JTextField();

textField.setBounds(150,110,300,30);

passwordField = new JPasswordField();

passwordField.setBounds(150,180,300,30);

JButton btnLogin = new JButton("Login");

btnLogin.setBounds(180,280,100,30);

Container c=getContentPane();

c.add(lblAdminLoginForm);

c.add(lblEnterName);

c.add(textField);

c.add(lblEnterPassword);

c.add(passwordField);

c.add(btnLogin);
btnLogin.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String name=textField.getText();

String
password=String.valueOf(passwordField.getPassword());

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

login lb=new login();

lb.setSize(500,500);

lb.setTitle("librarian section");

lb.setVisible(true);

else{

JOptionPane.showMessageDialog(
LibrarianLogin.this,"Sorry, Username or Password Error","Login Error!",
JOptionPane.ERROR_MESSAGE);

textField.setText("");

passwordField.setText("");

});

public static void main(String[] args) {


LibrarianLogin lb=new LibrarianLogin();

lb.setSize(500,500);

lb.setTitle("librarian login");

lb.setVisible(true);

class login extends JFrame {

login frame;

JPanel contentPane;

JButton add;

JButton view;

JButton issue;

JButton returnbk;

JButton logout;

public login() {
setLayout(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel lblAdminLoginForm = new JLabel("Library section");

lblAdminLoginForm.setBounds(130,0,500,100);

lblAdminLoginForm.setForeground(Color.GRAY);

lblAdminLoginForm.setFont(new Font("Arial", Font.PLAIN, 28));

add = new JButton("Add Books");

add.setBounds(80,110,300,30);

view = new JButton("View Books");

view.setBounds(80,180,300,30);

issue = new JButton("Issue Book");

issue.setBounds(80,250,300,30);

returnbk = new JButton("Return Book");

returnbk.setBounds(80,320,300,30);

logout = new JButton("Logout");


logout.setBounds(80,390,300,30);

Container c=getContentPane();

c.add(lblAdminLoginForm);

c.add(add);

c.add(view);

c.add(issue);

c.add(returnbk);

c.add(logout);

logout.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JOptionPane.showMessageDialog(login.this,"logout
successful");

System.exit(0);

});

}
}
Practical 2

package javaapplication1;

import java.sql.*;

public class JavaApplication1

{ static final String JDBC_DRIVER="com.mysql.jdbc.Driver";

static final String DB_URL="jdbc:mysql://localhost/sb1";

static final String USER="root";

static final String PASS="123456";

public static void main(String[] args)

{ Connection conn =null;

Statement stmt=null;

try{

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

System.out.println("Connecting to a selected database...");

conn=DriverManager.getConnection(DB_URL,USER,PASS);

System.out.println("Connected database successfully...");

System.out.println("Creating table in given database..");

stmt=conn.createStatement();

String sql="create table register(id varchar(20) PRIMARY KEY ,name varchar(20),course


varchar(20));";

stmt.executeUpdate(sql);

System.out.println("Created table in given database..");

stmt.executeUpdate("insert into register values(1,'saurabh','sycs');");


stmt.executeUpdate("create table book_register(book_no int(20) PRIMARY KEY,book_title
varchar(20),author_name varchar(20),quantity int(20),pub_name varchar(20));");

stmt.executeUpdate("insert into book_register values(1,'dbms','saurabh',2,'techmax');");

stmt.executeUpdate("create table issue_books(id INT NOT NULL REFERENCES register,book_no INT


NOT NULL REFERENCES book_register,book_title varchar(50),issue_date DATE, PRIMARY
KEY(id,book_no));");

stmt.executeUpdate("insert into issue_books values(1,1,'dbms','1998-06-28');");

stmt.executeUpdate("create table return_books(id INT NOT NULL REFERENCES register,book_no


INT NOT NULL REFERENCES book_register,book_title varchar(50),return_date DATE, PRIMARY
KEY(id,book_no));");

stmt.executeUpdate("insert into return_books values(1,1,'dbms','1998-06-30');");

}catch(SQLException se)

{ se.printStackTrace();

}catch(Exception e)

{ e.printStackTrace();

finally{

try{

if(stmt!=null)

conn.close();

catch(SQLException se)

{se.printStackTrace();

}
System.out.println("Goodbye!");

}
Prac 3 bussiness logic

package javaapplication2;

import java.sql.*;

public class JavaApplication2

{ static final String JDBC_DRIVER="com.mysql.jdbc.Driver";

static final String DB_URL="jdbc:mysql://localhost/sb4";

static final String USER="root";

static final String PASS="123456";

public static void main(String[] args)

{ Connection conn =null;

Statement stmt=null;

try{

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

System.out.println("Connecting to a selected database...");

conn=DriverManager.getConnection(DB_URL,USER,PASS);

System.out.println("Connected database successfully...");

System.out.println("Creating table in given database..");


stmt=conn.createStatement();

String sql="create table register(id varchar(20) PRIMARY KEY ,name


varchar(20),course varchar(20));";

stmt.executeUpdate(sql);

System.out.println("Created table in given database..");

stmt.executeUpdate("insert into register values(1,'saurabh','sycs');");

stmt.executeUpdate("insert into register values(2,'girish','sycs');");

stmt.executeUpdate("update register set name='rahul' where id=2");

stmt.executeUpdate("delete from register where id=1");

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

while(rs.next())

System.out.println(rs.getInt(1) + rs.getString(2) + rs.getString(3));

stmt.executeUpdate("create table book_register(book_no int(20) PRIMARY


KEY,book_title varchar(20),author_name varchar(20),quantity
int(20),pub_name varchar(20));");

stmt.executeUpdate("insert into book_register


values(1,'dbms','saurabh',2,'techmax');");

stmt.executeUpdate("insert into book_register


values(2,'web','girish',3,'techmax');");

stmt.executeUpdate("update register set name='girish' where id=2");

stmt.executeUpdate("delete from register where id=1");


ResultSet rs2=stmt.executeQuery("select * from book_register");

while(rs2.next())

System.out.println(rs2.getInt(1) + rs2.getString(2) + rs2.getString(3) +


rs2.getInt(4) + rs2.getString(5));

stmt.executeUpdate("create table issue_books(id INT NOT NULL REFERENCES


register,book_no INT NOT NULL REFERENCES book_register,book_title
varchar(50),issue_date DATE, PRIMARY KEY(id,book_no));");

stmt.executeUpdate("insert into issue_books values(1,1,'dbms','1998-06-


28');");

stmt.executeUpdate("insert into issue_books values(2,2,'web','1999-07-


23');");

stmt.executeUpdate("update issue_books set book_title='java' where id=2


and book_no=2");

stmt.executeUpdate("delete from issue_books where id=1 and book_no=1");

ResultSet rs3=stmt.executeQuery("select * from issue_books");

while(rs3.next())

System.out.println(rs3.getInt(1) + rs3.getInt(2) + rs3.getString(3) +


rs3.getString(4));

}
stmt.executeUpdate("create table return_books(id INT NOT NULL
REFERENCES register,book_no INT NOT NULL REFERENCES
book_register,book_title varchar(50),return_date DATE, PRIMARY
KEY(id,book_no));");

stmt.executeUpdate("insert into return_books values(1,1,'dbms','1998-06-


30');");

stmt.executeUpdate("insert into return_books values(2,2,'web','1999-07-


30');");

stmt.executeUpdate("update return_books set book_title='java' where id=2


and book_no=2");

stmt.executeUpdate("delete from return_books where id=1 and


book_no=1");

ResultSet rs4=stmt.executeQuery("select * from return_books");

while(rs4.next())

System.out.println(rs4.getInt(1) + rs4.getInt(2) + rs4.getString(3) +


rs4.getString(4));

}catch(SQLException se)

{ se.printStackTrace();

}catch(Exception e)

{ e.printStackTrace();

}
finally{

try{

if(stmt!=null)

conn.close();

catch(SQLException se)

{se.printStackTrace();

System.out.println("Goodbye!");

Output-
Practical 4

To store the image in mysql

package javaapplication13;

import java.sql.*;

import java.io.*;

public class JavaApplication13 {

public static void main(String args[]){

try{

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

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/demo1","root","123456");

File file=new File("C:\\Users\\sys\\Desktop\\desert.jpg");

FileInputStream fis=new FileInputStream(file);


PreparedStatement ps=con.prepareStatement("insert into image_table (name,image)
values(?,?)");

ps.setString(1,"image 1");

ps.setBinaryStream(2,fis,(int)file.length());

ps.executeUpdate();

ps.close();

fis.close();

con.close();

}catch(Exception e){

e.printStackTrace();

To retrieve the image from mysql on location like desktop with other name

package javaapplication14;

import java.sql.*;

import java.io.*;

public class JavaApplication14{

public static void main(String[] args) {

try{

Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(

"jdbc:mysql://localhost/demo1","root","123456");

PreparedStatement ps=con.prepareStatement("select * from image_table");

ResultSet rs=ps.executeQuery();

if(rs.next()){//now on 1st row

Blob b=rs.getBlob(2);//2 means 2nd column data

byte barr[]=b.getBytes(1,(int)b.length());//1 means first image

FileOutputStream fout=new FileOutputStream("C:\\Users\\sys\\Desktop\\desert1.jpg");

fout.write(barr);

fout.close();

}//end of if

System.out.println("ok");

con.close();

}catch (Exception e) {e.printStackTrace(); }

Output-
Practical 5

Servlet life cycle

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class ServletLifeCycle extends HttpServlet

public void init(ServletConfig config) throws ServletException

System.out.println("am from init method");

public void doGet(HttpServletRequest req,HttpServletResponse res)throws


ServletException,IOException

res.setContentType("text/html");

PrintWriter pw=res.getWriter();

pw.println("i am from doget method");

pw.close();

public void destroy()


{

System.out.println("am from destroy method");

Output-
CRUD OPERATION IN SERVLET

Practical 6

Advance java

import java.io.*;

import java.sql.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class ServletLifeCycle extends HttpServlet

public void doGet(HttpServletRequest r1,HttpServletResponse r2) throws IOException,


ServletException

r2.setContentType("text/html");

PrintWriter pw=r2.getWriter();

ResultSet rs;

try

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

Connection con=DriverManager.getConnection("jdbc:mysql://localhost/EMP","root","123456");

Statement st=con.createStatement();

st.executeUpdate("create table employees(id int(20),name varchar(60))");

pw.println("table created successfully");


pw.println("<br>");

st.executeUpdate("insert into employees values(1,'saurabh')");

pw.println("record1 inserted successfully");

pw.println("<br>");

st.executeUpdate("insert into employees values(4,'pratik')");

pw.println("record2 inserted successfully");

pw.println("<br>");

st.executeUpdate("delete from employees where id=4");

pw.println("record deleted successfully");

pw.println("<br>");

st.executeUpdate("update employees set name='saurabh' where id=4");

pw.println("record updated successfully");

pw.println("<br>");

rs=st.executeQuery("select * from employees");

while (rs.next())

pw.println(rs.getInt(1) + rs.getString(2));

pw.println("<br>");

con.close();

catch(ClassNotFoundException e)

{pw.println("helli");

catch(SQLException e)

{pw.println("hi");
}

pw.close();

Output=
Prac 7

Index.html

<html>

<head>

<title> jsp page</title>

</head>

<body>

<form method="post" action="tableshow.jsp">

Enter table name:

<input type="text" name="t1"/><br><br>

<input type="submit"/>

</form>

</body>

</html>

Tableshow.jsp

<%@page import="java.sql.DriverManager"%>

<%@page import="java.sql.ResultSetMetaData"%>

<%@page import="java.sql.ResultSet"%>

<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>

<%@page contentType="text/html" %>

<html>

<head>

<title> jsp page</title>

</head>

<body>

<%

int count=0;

String tablename;

Connection con;

Statement st;

ResultSet rs;

ResultSetMetaData rsmd;

try

tablename=request.getParameter("t1");

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

con=DriverManager.getConnection("jdbc:mysql://localhost/EMP","root","123456");

if(tablename.equals("employees"))

st=con.createStatement();

st.executeUpdate("insert into employees values(3,'rohi',50000)");

out.println("<br> <br> <h3>succesfully inserted the values in table employees </h3>");


st.executeUpdate("delete from employees where name='girish'");

out.println("<br> <h3>successfully deleted row where name=girish </h3>");

st.executeUpdate("update employees set id=9 where name='saurabh'");

out.println("<br> <h3>successfully updated table where name=saurabh </h3>");

rs=st.executeQuery("select * from employees");

rsmd=rs.getMetaData();

%>

<table border="2">

<tr>

<td><%=rsmd.getColumnLabel(1)%></td>

<td><%=rsmd.getColumnLabel(2)%></td>

<td><%=rsmd.getColumnLabel(3)%></td>

</tr>

<%

while(rs.next())

count=count+1;

%>

<tr>

<td>

<%=rs.getInt("id")%>

</td>

<td>

<%=rs.getString("name")%>

</td>

<td>

<%=rs.getInt("salary")%>
</td>

</tr>

<%

} %>

</table>

<%

else

out.println("Specified table not present");

catch(Exception e)

out.println(e);

%>

<%

out.println("<br> <br> <h3> Total number of Records: "+count+" </h3>");

%>

</body>

</html>

Output—
cmd---
Practical 8

Student.java

Package abc;

Public class Student{

private int rno,marks;

private String sname;

public Student()

public int getMarks()

return 99;

public String getSname()

return "ram";

public int getRno()


{

return 12;

Index.jsp

<html>

<body>

<jsp:useBean class="abc.Student" id="stu"/>

<pre>

Time:<jsp:getProperty name="stu"

property="sname"/><br>

Hour:<jsp:getProperty name="stu" property="rno"/><br>

Minute:<jsp:getProperty name="stu"

property="marks"/><br>
</pre>

</body>

</html>

Output-
Java practical 10

import org.json.simple.JSONObject;

public class JSON_ENCODE_DECODE

public static void main(String[] args)

JSONObject obj=new JSONObject();

System.out.println("encoding......\n");

obj.put("COLLEGE NAME","ABC COLLEGE");

obj.put("YEAR OF ESTD",new Integer(1927));

obj.put("NO OF COURSES",new Double(60));

System.out.println(obj);

}
import java.util.HashMap;

import java.util.Map;

import org.json.simple.JSONObject;

import org.json.simple.JSONValue;

public class JSON_USING_MAP

public static void main(String[] args)

Map obj=new HashMap();

System.out.println("encoding using map....\n");

obj.put("COLLEGE NAME","ABC COLLEGE");

obj.put("YEAR OF ESTD",new Integer(1927));

obj.put("NO OF COURSES",new Double(60));

String txt=JSONValue.toJSONString(obj);

System.out.println(txt);

}
import org.json.simple.JSONObject;

import org.json.simple.JSONValue;

public class JSON_DECODE {

public static void main(String[] args)

String s="{\"COLLEGE NAME\":\"XYZ COLLEGE\",\"YEAROF ESTD\":1987.0,\"NO OF


COURSES\":56}";

Object obj=JSONValue.parse(s);

JSONObject jsObj=(JSONObject)obj;

String col_name=(String)jsObj.get("COLLEGE NAME");

Double year=(Double)jsObj.get("YEAR OF ESTD");

Long courses=(Long)jsObj.get("NO OF COURSES");

System.out.println(col_name+"\n"+year+"\n"+courses);

Anda mungkin juga menyukai