Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

ENTERPRISE COMPUTING WITH

JAVA LAB
FILE
BCE – C –651

Submitted By: Submitted to:


Aayush Verma Mr. Aman Tyagi
rd th
CSE, 3 year, 6 Sem Assistant Professor
Exam no. – 186301004 CSE Department, Fet, Gkv
Class Rollno. – 04
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
FACULTY OF ENGINEERING AND TECHNOLOGY
GURUKUL KANGRI UNIVERSITY
2020-2021

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


Certificate by student

This is to certify that the programs in this file are compiled and executed

by me, and I have not copied them from anywhere. This work is done by

me and I have not presented it anywhere else.

Aayush Verma
Exam no. – 186301004
Class Rollno. – 04

Certificate by Teacher

This is to certify that the programs in this file are done under my

supervision and are not being copied from anywhere or presented

anywhere for any sort of benefit.

Mr. AMAN TYAGI

Assistant Professor
Department of Computer Science & Engineering
Faculty of Engineering and Technology

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


INDEX

S.No. Experiment Signature


1. Write a program to connect a java program to
database.

2. Write a program to implement Prepared


statement in database.

3. Write a program to implement ResultSet methods


like navigating a resultset, updating a resultset,
etc.

4. Write a program to create a application using


RMI.

5. Write a program to show java networking by


creating communication between client and
server.

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


PROGRAM-1
WRITE A PROGRAM TO CONNECT A JAVA PROGRAM TO
DATABASE.

PROGRAM:

/*

CREATING DATABASE, SELECTING DATABASE, CREATING TABLE,


INSERTING RECORD, SELECTING RECORD, UPDATING RECORD,
DELETING RECORD

*/

import java.sql. *;
class Example2
{
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static String DB_URL = "jdbc:mysql://localhost:3306/";

public static void main(String args[])


{
try{
Class.forName(JDBC_DRIVER);

Connection con = DriverManager.getConnection(DB_URL,"host","host");

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


//Creating database...

System.out.println("Creating database...");
Statement stmt = con.createStatement();

String sql = "CREATE DATABASE STUDENTS";


stmt.executeUpdate(sql);
System.out.println("Database created successfully...");
con.close();

//selection Database
DB_URL+="STUDENTS";
con = DriverManager.getConnection(DB_URL,"namit","namit");

//CREATING TABLE
System.out.println("Creating table in given database...");
stmt = con.createStatement();

String query = "CREATE TABLE REGISTRATION " +


"(id INTEGER not NULL, " +
" first VARCHAR(255), " +
" last VARCHAR(255), " +
" age INTEGER, " +
" PRIMARY KEY ( id ))";

stmt.executeUpdate(query);
AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER
System.out.println("Created table in given database...");

//INSERTING RECORDS
System.out.println("Inserting records into the table...");
//stmt = conn.createStatement();

String rec1 = "INSERT INTO Registration " +


"VALUES (100, 'Zara', 'Ali', 18)";
stmt.executeUpdate(rec1);

//USING BATCH COMMAND


String rec2 = "INSERT INTO Registration " + "VALUES (101,
'Mahnaz', 'Fatma', 25)";
stmt.addBatch(rec2);
String rec3 = "INSERT INTO Registration " +"VALUES (102, 'Zaid',
'Khan', 30)";
stmt.addBatch(rec3);
String rec4 = "INSERT INTO Registration " + "VALUES(103, 'Sumit',
'Mittal', 28)";
stmt.addBatch(rec4);
String rec5 = "INSERT INTO Registration " + "VALUES(104,
'Suresh', 'Mehta', 32)";
stmt.addBatch(rec5);
String rec6 = "INSERT INTO Registration " + "VALUES(105,
'Rajesh', 'Singh', 22)";
stmt.addBatch(rec6);
String rec7 = "INSERT INTO Registration " + "VALUES(106,
'Mukesh', 'Kohli', 26)";
stmt.addBatch(rec7);

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


stmt.executeBatch();
Thread.sleep(1000);
System.out.println("Inserted records into the table...");

//EXECUTING SELECT QUERY TO DISPLAY RECORDS


System.out.println("Executing select query...");
//stmt = conn.createStatement();

String show = "SELECT id, first, last, age FROM Registration";


ResultSet rs = stmt.executeQuery(show);
System.out.println("Extracting data from result set.....");
Thread.sleep(1000);
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");

//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
rs.close();
AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER
Thread.sleep(1000);

//UPDATING ROWS
System.out.println("Updating Registration table for id 100 and
101.....");
String change = "UPDATE Registration " + "SET age = 30 WHERE id in
(100, 101)";
stmt.executeUpdate(change);

System.out.println("Records Updated...");
// Now you can extract all the records
// to see the updated records
System.out.println("Executing select query to check the updates...");
Thread.sleep(1000);
show = "SELECT id, first, last, age FROM Registration";
rs = stmt.executeQuery(show);

while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");

//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
rs.close();

//DELETING ROW
System.out.println("Deleting row for id =101.....");
Thread.sleep(1000);
String del = "DELETE FROM Registration " + "WHERE id = 101";
stmt.executeUpdate(del);
System.out.println("Record Deleted....");

// Now you can extract all the records


// to see the remaining records
System.out.println("Executing select query to check the deleted
record...");
Thread.sleep(1000);
del = "SELECT id, first, last, age FROM Registration";
rs = stmt.executeQuery(del);

while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
rs.close();
con.close();

}catch(Exception e){System.out.println(e);}
}
}

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


OUTPUT:

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


PROGRAM-2
WRITE A PROGRAM TO IMPLEMENT PREPARED STATEMENT IN
DATABASE.

PROGRAM:

import java.sql.*;

class DBprepareSt{

public static void main(String[] args) {

try{

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

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/STUDE
NTS","root","root");

PreparedStatement st=con.prepareStatement("INSERT INTO


REGISTRATION VALUES(?,?,?,?)");

st.setInt(1,107);

st.setString(2,"Rahul");

st.setString(3,"Sharma");

st.setInt(4,24);

st.executeUpdate();

System.out.println("Data Inserted.");

String query="SELECT * FROM REGISTRATION";

ResultSet rs=st.executeQuery(query);

while(rs.next()){

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


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

con.close();

}catch(Exception e){System.out.println(e);}

OUTPUT:

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


PROGRAM-3
WRITE A PROGRAM TO IMPLEMENT RESULTSET METHODS LIKE
NAVIGATING A RESULTSET, UPDATING A RESULTSET, ETC.

PROGRAM:

/*METHODS OF MOVE IN RESULTSET

AND DROPING DATABASE

next: Moves the cursor forward one row. Returns true if the cursor is now
positioned on a row and false if the cursor is positioned after the last
row.

previous: Moves the cursor backward one row. Returns true if the cursor
is now positioned on a row and false if the cursor is positioned before
the first row.

first: Moves the cursor to the first row in the ResultSet object. Returns
true if the cursor is now positioned on the first row and false if the
ResultSet object does not contain any rows.

last:: Moves the cursor to the last row in the ResultSet object. Returns
true if the cursor is now positioned on the last row and false if the
ResultSet object does not contain any rows.

beforeFirst: Positions the cursor at the start of the ResultSet object,


before the first row. If the ResultSet object does not contain any rows,
this method has no effect.

afterLast: Positions the cursor at the end of the ResultSet object, after
the last row. If the ResultSet object does not contain any rows, this
method has no effect.

relative(int rows): Moves the cursor relative to its current position.

absolute(int row):Positions the cursor on the row specified by the


parameter row.

*/

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


import java.sql.*;
class ResultsetNav
{
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static String DB_URL = "jdbc:mysql://localhost:3306/STUDENTS";

public static void main(String args[])


{
try{
Class.forName(JDBC_DRIVER);

Connection con =
DriverManager.getConnection(DB_URL,"namit","namit");
Statement stmt = con.createStatement();
//MOVING THROUGH THE RESULTSET
stmt =
con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultS
et.CONCUR_READ_ONLY);
String sel = "SELECT id, first, last, age FROM Registration";
ResultSet rs = stmt.executeQuery(sel);

System.out.println("Moving to first row...using next()...");


rs.next(); // row1
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3)+" "+rs.getInt(4));
System.out.println("Moving to second row...using next()...");
rs.next(); // row2
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3)+" "+rs.getInt(4));
AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER
System.out.println("Moving to third row...using next()...");
rs.next(); // row2
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3)+" "+rs.getInt(4));
System.out.println("Moving a row backwards...using previous()...");
rs.previous();// moving one row backwards
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3)+" "+rs.getInt(4));
System.out.println("Moving to first row...using first()...");
rs.first();
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3)+" "+rs.getInt(4));
System.out.println("Moving to last row...using last()...");
rs.last();
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3)+" "+rs.getInt(4));
System.out.println("Moving to default position ...using
beforeFirst()...");
rs.beforeFirst();
rs.next();
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3)+" "+rs.getInt(4));
System.out.println("Moving from current position to 3 row...using
relative()...");
rs.relative(3);
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3)+" "+rs.getInt(4));
System.out.println("Moving to second row...using absolute()...");
rs.absolute(2);

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


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

rs.close();

//DROPING DATABASE
System.out.println("Deleting database...");

String drp = "DROP DATABASE STUDENTS";


stmt.executeUpdate(drp);
System.out.println("Database deleted successfully...");

con.close();

}catch(Exception e){System.out.println(e);}
}
}

OUTPUT:

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER
PROGRAM-4
WRITE A PROGRAM TO CREATE AN APPLICATION USING RMI.

PROGRAM:

1) create the remote interface

import java.rmi.*;

public interface Adder extends Remote{

public int add(int x,int y)throws RemoteException;

2) Provide the implementation of the remote interface

import java.rmi.*;

import java.rmi.server.*;

public class AdderRemote extends UnicastRemoteObject implements A


dder{

AdderRemote()throws RemoteException{

super();

public int add(int x,int y){return x+y;}

3) create the stub and skeleton objects using the rmic tool.

rmic AdderRemote

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


4) Start the registry service by the rmiregistry tool

rmiregistry 5000

5) Create and run the server application

import java.rmi.*;

import java.rmi.registry.*;

public class MyServer{

public static void main(String args[]){

try{

Adder stub=new AdderRemote();

Naming.rebind(“rmi://localhost:3000/aayush”,stub);

}catch(Exception e){System.out.println(e);}

6) Create and run the client application

import java.rmi.*;

public class MyClient{

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


public static void main(String args[]){

try{

Adder stub=(Adder)Naming.lookup(“rmi://localhost:3000/aayush”);

System.out.println(stub.add(34,4));

}catch(Exception e){}

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER


PROGRAM-5
WRITE A PROGRAM TO SHOW JAVA NETWORKING BY CREATING
COMMUNICATION BETWEEN CLIENT AND SERVER.

PROGRAM:

//Server.java

import java.io.*;

import java.net.*;

public class Server {

public static void main(String[] args){

try{

ServerSocket ss=new ServerSocket(3000);

Socket s=ss.accept();//establishes connection

DataInputStream dis=new DataInputStream(s.getInputStream());

String str=(String)dis.readUTF();

System.out.println("message= "+str);

ss.close();

}catch(Exception e){System.out.println(e);}

//Client.java

import java.io.*;

import java.net.*;

public class Client {

public static void main(String[] args) {


AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER
try{

Socket s=new Socket("localhost",3000);

DataOutputStream dout=new DataOutputStream(s.getOutputStream());

dout.writeUTF("Hello Server");

dout.flush();

dout.close();

s.close();

}catch(Exception e){System.out.println(e);}

OUTPUT:

AAYUSH VERMA ROLL NO.:186301004 B.TECH CSE 6th SEMESTER

You might also like