Assignment -3 Source Code. Student.java

Similar documents
Assignment-1 Final Code. Student.java

CreateServlet.java

Tiers (or layers) Separation of concerns

WEB SERVICES EXAMPLE 2

while (rs.next()) { String[] temp_array = {"","",""}; int prodid = rs.getint(1); temp_array[0] = ""+prodid;

Distributed Systems Project 3 Assigned: Wednesday, October 2, 2013 Due Friday Oct 25, 11:59 PM

SQream Connector JDBC SQream Technologies Version 2.9.3

Previous to Chapter 7 Files

Complimentary material for the book Software Engineering in the Agile World

Author - Ashfaque Ahmed

THIS EXAMINATION PAPER MUST NOT BE REMOVED FROM THE EXAMINATION ROOM

EJB - ACCESS DATABASE

1. PhP Project. Create a new PhP Project as shown below and click next

Lecture 7. File Processing

WINDOWS SOCKET PROGRAMMING

CPSC 441 Tutorial TCP Server. Department of Computer Science University of Calgary

package p1; public class Derivation extends Protection { public Derivation() { System.out.println("Derived class constructor");

12% of course grade. CSCI 201L Final - Written Fall /7

Object-Oriented Programming in Java


Distributed Systems Project 5 Assigned: Friday, April 6, 2012 Due: Friday, April 20, 11:59:59 PM

Software Practice 1 - Socket

Network. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

Exceptions Binary files Sequential/Random access Serializing objects

Java Database Connectivity

Networking Code CSCI 201 Principles of Software Development

Mobile Computing. MIDTERM EXAM #1 Due date: Nov.02, 2018 NOTE: WRITE YOUR ANSWERS IN ENGLISH. ID#: Name: (Kor.) (Eng.)

JDBC, Transactions. Niklas Fors JDBC 1 / 38

CSC System Development with Java. Database Connection. Department of Statistics and Computer Science. Budditha Hettige

Internet Technology 2/7/2013

Socket Programming(TCP & UDP) Sanjay Chakraborty

JAVA - NETWORKING (SOCKET PROGRAMMING)

import org.simpleframework.xml.default; import org.simpleframework.xml.element; import org.simpleframework.xml.root;

Accessing databases in Java using JDBC

Sit-In Lab 2 - OOP Championship

CSE 660 Lab 7. Submitted by: Arumugam Thendramil Pavai. 1)Simple Remote Calculator. Server is created using ServerSocket class of java. Server.

CMPS 11 Introduction to Programming Midterm 1 Review Problems

Object Oriented Programming

Prof. Edwar Saliba Júnior

Java SE 8 Programmer I and II Syballus( Paper codes : 1z0-808 & 1z0-809)

Introduction to Computer Science Unit 2. Notes

Unit 3 - Java Data Base Connectivity

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 9. Nadia Polikarpova

LAMPIRAN. public class PengajuanDana extends javax.swing.jinternalframe { Connection conn = null; ResultSet rs = null; PreparedStatement ps = null;

AP COMPUTER SCIENCE A

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

Distributed Objects. Remote Method Invokation

Visit for more.

Advanced Programming Methods. Seminar 12

public static void main(string[] args) throws IOException { sock = new Socket(args[0], Integer.parseInt(args[1]));

Prof. Edwar Saliba Júnior

Software 1 with Java. Recitation No. 7 (Java IO) May 29,

M257 Past Paper Oct 2008 Attempted Solution

Software 1 with Java. Recitation No. 9 (Java IO) December 10,

AN IMPROTANT COLLECTION OF JAVA IO RELATED PROGRAMS

CS 251 Intermediate Programming Java I/O Streams

CN208 Introduction to Computer Programming

Introduction to Computer Science Unit 2. Notes

agentmom User's Manual

Software 1. Java I/O

Java Programming Lecture 9

You write standard JDBC API application and plug in the appropriate JDBC driver for the database the you want to use. Java applet, app or servlets

CS September 2017

DATABASE DESIGN I - 1DL300

CSE 501N Final Fall Points Possible

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

UNIT-3 Java Database Client/Server

Lecture 8 Chapter 4: Inter-process Communications

ANN exercise session

Java. Java. Java. Java. java. smalltalk. java. java virtual machine. java. C++ virtual. interface C++ Operator overloading Template C++ test.

What is Serialization?

Many Stream Classes. File I/O - Chapter 10. Writing Text Files. Text Files vs Binary Files. Reading Text Files. EOF with hasmoreelements()

I/O Streams. Object-oriented programming

Université Antonine - Baabda

School of Informatics, University of Edinburgh

Asynchronous Communication using Messaging

COMP 213. Advanced Object-oriented Programming. Lecture 20. Network Programming

Oracle 1z Java Standard Edition 5 Programmer Certified Professional Upgrade Exam. Practice Test. Version:

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

Unit 2 JDBC Programming

Static, Final & Memory Management

AP CS Unit 3: Control Structures Notes

1Z Java SE 7 Programmer II Exam Summary Syllabus Questions

Principles, Models, and Applications for Distributed Systems M

Core Java Contents. Duration: 25 Hours (1 Month)

Java Database Connectivity

CCHS Math Recursion Worksheets M Heinen CS-A 12/5/2013. Recursion Worksheets Plus Page 1 of 6

Lecture 2. Introduction to JDBC

HL7 FHIR Data Consolidation Tool

CSC 231 DYNAMIC PROGRAMMING HOMEWORK Find the optimal order, and its optimal cost, for evaluating the products A 1 A 2 A 3 A 4

King Saud University College of Computer and Information Systems Department of Computer Science CSC 113: Java Programming-II, Spring 2016

Core Java Syllabus DAY -1 :

J2ME With Database Connection Program

JDBC [Java DataBase Connectivity]

CREATE A SERVLET PROGRAM TO DISPLAY THE STUDENTS MARKS. To create a servlet program to display the students marks

CS 351 Design of Large Programs Sockets Example

CS 2113 Software Engineering

Distributed Systems Recitation 1. Tamim Jabban

UNIT-VIII Introduction of Java Database Connectivity

Objec&ves. Review. Standard Error Streams

Transcription:

import java.io.serializable; Assignment -3 Source Code Student.java public class Student implements Serializable{ public int rollno; public String name; public double marks; public Student(int rollno, String name, double marks) { this.rollno = rollno; this.name = name; this.marks = marks; @Override public String tostring() { return "Student [rollno=" + rollno + ", name=" + name + ", marks=" + marks + "]"; Operations.java import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.util.arraylist; public class Operations { int addstudent(student st) {

int rowsinserted = 0; Connection con = DB.getConnection(); PreparedStatement ps = con.preparestatement("insert into Student(rollNo, name, marks) values(?,?,?)"); ps.setint(1, st.rollno); ps.setstring(2, st.name); ps.setdouble(3, st.marks); rowsinserted = ps.executeupdate(); con.close(); catch (SQLException e) { return rowsinserted; int deletestudent(int rollno) { int rowsdeleted = 0; Connection con = DB.getConnection(); PreparedStatement ps = con.preparestatement("delete from Student where rollno =?"); ps.setint(1, rollno); rowsdeleted = ps.executeupdate(); con.close(); catch (SQLException e) {

return rowsdeleted; Student searchstudent(int rollno){ Student st = null; Connection con = DB.getConnection(); PreparedStatement ps = con.preparestatement("select * from Student where rollno =?"); ps.setint(1, rollno); ResultSet rs = ps.executequery(); if(rs.next()){ st = new Student(rs.getInt("rollNo"), rs.getstring("name"), rs.getdouble("marks")); con.close(); catch (SQLException e) { return st; ArrayList<Student> failedstudents(double pm){ ArrayList<Student> failed = new ArrayList<Student>(); Connection con = DB.getConnection();

PreparedStatement ps = con.preparestatement("select * from Student where marks <?"); ps.setdouble(1, pm); ResultSet rs = ps.executequery(); while(rs.next()){ Student st = new Student(rs.getInt("rollNo"), rs.getstring("name"), rs.getdouble("marks")); failed.add(st); con.close(); catch (SQLException e) { return failed; ArrayList<Student> display(){ ArrayList<Student> alldata = new ArrayList<Student>(); Connection con = DB.getConnection(); PreparedStatement ps = con.preparestatement("select * from Student"); ResultSet rs = ps.executequery(); while(rs.next()){ Student st = new Student(rs.getInt("rollNo"), rs.getstring("name"), rs.getdouble("marks")); alldata.add(st); con.close();

catch (SQLException e) { return alldata; Client.java import java.io.datainputstream; import java.io.dataoutputstream; import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import java.net.socket; import java.util.arraylist; import java.util.scanner; public class Client { @SuppressWarnings("unchecked") public static void main(string[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); Socket cs = new Socket("localhost", 5000); DataOutputStream dout = new DataOutputStream(cs.getOutputStream()); DataInputStream din = new DataInputStream(cs.getInputStream()); ObjectOutputStream oout = new ObjectOutputStream( cs.getoutputstream());

ObjectInputStream oin = new ObjectInputStream(cs.getInputStream()); ArrayList<Student> al =null; int ch; String name; int rollno; double marks; // Student class of Client Student st = null; while (true) { System.out.print("\n***** Select Operation *****\n"); System.out.print("\n\t1. Add Student\n\t2. Delete Student\n\t3. Search Student\n\t4. Failed Student\n\t5. Display\n\t6. Exit"); System.out.print("\nEnter choice: "); ch = sc.nextint(); dout.writeint(ch); switch (ch) { case 1: System.out.print("\nEnter Roll No - "); rollno = sc.nextint(); System.out.print("\nEnter Name - "); name = sc.next(); System.out.print("\nEnter Marks - "); marks = sc.nextdouble(); st = new Student(rollNo, name, marks); oout.writeobject(st); al = (ArrayList<Student>) oin.readobject(); System.out.print("\nAdded Succesfully!!!\nUpdated Students List -> \n");

s.name + " -:- " + s.marks); for(student s : al){ System.out.println(s.rollNo + " -:- " + break; case 2: System.out.print("\nEnter rollno to be deleted : "); rollno = sc.nextint(); dout.writeint(rollno); al = (ArrayList<Student>) oin.readobject(); System.out.print("\nAdded Succesfully!!!\nUpdated Students List -> \n"); for(student s : al){ System.out.println(s.rollNo + " -:- " + s.name + " -:- " + s.marks); break; case 3: System.out.print("\nEnter rollno to be searched : "); rollno = sc.nextint(); dout.writeint(rollno); st = (Student) oin.readobject(); if (st!= null) { System.out.print("\nSearched Student -> " + st); else { System.out.println("User not found!!!"); break; case 4:

"); System.out.print("\nEnter passing marks : double pm = sc.nextdouble(); dout.writedouble(pm); \n"); al = (ArrayList<Student>) oin.readobject(); System.out.print("\nFailed Students List -> for(student s : al){ System.out.println(s.rollNo + " -:- " + s.name + " -:- " + s.marks); break; case 5: al = (ArrayList<Student>) oin.readobject(); System.out.print("\nStudents List -> \n"); for(student s : al){ System.out.println(s.rollNo + " -:- " + s.name + " -:- " + s.marks); break; if (ch == 6) { System.out.println(din.readUTF()); break; catch (IOException e) { catch (ClassNotFoundException e) {

import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; DB.java public class DB { static Connection con = null; public static Connection getconnection() { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/xyz", "root", "root123"); catch (SQLException e) { catch (ClassNotFoundException e) { return con; public static void closeconnection(){ con.close(); catch (SQLException e) {

ServerDemo.java import java.io.datainputstream; import java.io.dataoutputstream; import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import java.net.serversocket; import java.net.socket; import java.util.arraylist; import java.util.concurrent.executorservice; import java.util.concurrent.executors; public class ServerDemo { int port; ServerSocket ss = null; Socket cs = null; Operations obj = null; ExecutorService pool = null; public ServerDemo(int port){ this.port = port; obj = new Operations(); pool = Executors.newFixedThreadPool(5); public void startserver(){ //creating one server socket ss = new ServerSocket(5000); //for accepting multiple clients while(true){ System.out.println("Server waiting for client..."); cs = ss.accept();

System.out.println("Got one client.creating thread for this client..."); ServerThreadBody runnable = new ServerThreadBody(cs, obj, this); //assigning thread to pool pool.execute(runnable); catch (IOException e) { public static void main(string[] args) { // TODO Auto-generated method stub new ServerDemo(5000).startServer(); class ServerThreadBody implements Runnable{ ServerDemo server = null; Socket cs = null; DataOutputStream dout = null; DataInputStream din = null; ObjectOutputStream oout = null; ObjectInputStream oin = null; Student st = null; Operations obj = null; { ServerThreadBody(Socket cs, Operations obj, ServerDemo server) // TODO Auto-generated constructor stub

this.cs = cs; this.obj = obj; this.server = server; @Override public void run() { // TODO Auto-generated method stub try{ dout = new DataOutputStream(cs.getOutputStream()); din = new DataInputStream(cs.getInputStream()); oout = new ObjectOutputStream( cs.getoutputstream()); oin = new ObjectInputStream(cs.getInputStream()); int rollno; while (true) { client int ch = din.readint(); if (ch == 1) { // Reading com.server.student Object from st = (Student) oin.readobject(); // Calling addstudent() of com.server.operations class obj.addstudent(st); oout.writeobject(obj.display()); else if (ch == 2) { // Reading rollno from client for deleting

rollno = din.readint(); // Calling addstudent() of com.server.operations class obj.deletestudent(rollno); oout.writeobject(obj.display()); else if (ch == 3) { // Reading rollno from client for deleting rollno = din.readint(); // Calling addstudent() of com.server.operations class st = obj.searchstudent(rollno); if (st!= null) { // Writing searched Student object back to client oout.writeobject(st); else { // Writing ArrayList al object back to client oout.writeobject(null); else if (ch == 4) { // Reading rollno from client for deleting double pm = din.readdouble(); // Calling addstudent() of com.server.operations class ArrayList<Student> failed = obj.failedstudents(pm); oout.writeobject(failed); else if (ch == 5) { oout.writeobject(obj.display()); else if(ch == 6){

dout.writeutf("bye Bye Client from server!!! "); System.out.println("Socket Closed!!!"); cs.close(); break; catch (IOException e) { catch (ClassNotFoundException e) { *********************************************************