Sunteți pe pagina 1din 5

EXP: NO.

Simple OPAC system for library

AIM: To develop a simple OPAC system for library using event-driven and concurrent

programming paradigms of Java. JDBC is used to connect to a back-end database. ALGORITHM: Step 1: Start the program. Step 2: Design the front end for the library system. Step 3: Connect the front end with the database at the backend using JDBC. Step 4: Design the front end such that it accepts the inputs from the user and inserts the records into the database. Step 5: Display the contents of the database at the front end. Step 6: Suspend the established connections. Step 7: Stop the program. PROGRAM: Datas.java import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Datas extends JFrame implements ActionListener { JTextField id; JTextField name; JButton next; JButton addnew; JPanel p; static ResultSet res; static Connection conn; static Statement stat; public Datas() { super("Our Application"); Container c = getContentPane(); c.setLayout(new GridLayout(5,1)); id = new JTextField(20); name = new JTextField(20); next = new JButton("Next BOOK"); p = new JPanel(); c.add(new JLabel("ISBN",JLabel.CENTER)); c.add(id); c.add(new JLabel("Book Name",JLabel.CENTER)); c.add(name); c.add(p); p.add(next); next.addActionListener(this); pack(); setVisible(true); addWindowListener(new WIN()); } public static void main(String args[]) { Datas d = new Datas(); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection("jdbc:odbc:custo"); stat = conn.createStatement(); res = stat.executeQuery("Select * from stu"); res.next(); } catch(Exception e) { System.out.println("Error" +e); } d.showRecord(res); } public void actionPerformed(ActionEvent e)

if(e.getSource() == next) { try { res.next(); catch(Exception ee) {} showRecord(res); }

} public void showRecord(ResultSet res) { try { id.setText(res.getString(2)); name.setText(res.getString(3)); } catch(Exception e) {} } class WIN extends WindowAdapter { public void windowClosing(WindowEvent w) { JOptionPane jop = new JOptionPane(); jop.showMessageDialog(null,"Database","Thanks",JOptionPane.QUESTION_MESSAGE); } }} Prog1.java import java.sql.*; import java.sql.DriverManager.*; class Ja { String bookid,bookname; int booksno; Connection con; Statement stmt; ResultSet rs; Ja( ) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:co"); } catch(Exception e) { System.out.println("connection error"); } } void myput() { try { stmt=con.createStatement(); rs=stmt.executeQuery("SELECT * FROM opac"); while(rs.next()) { booksno=rs.getInt(1); bookid=rs.getString(2); bookname=rs.getString(3); System.out.println("\n"+ booksno+"\t"+bookid+"\t"+bookname);} rs.close(); stmt.close(); con.close(); } catch(SQLException e) { System.out.println("sql error"); } }}

class prog1 { public static void main(String arg[]) { Ja j=new Ja(); j.myput(); } } RESULT: Thus the program to design a simple OPAC system for library was executed and the output was verified successfully. EXP: NO. 10 Multithreaded Echo Server and Client AIM: To develop a multithreaded echo server and a corresponding client. ALGORITHM: Step 1: Start the program. Step 2: Create a Socket at the Server side. Step 3: Design the server such that it responds to each client using a separate thread. Step 4: The server receives the data sent by the client and echoes those data. Step 5: Provide the necessary exception handling mechanisms at the server. Step 6: Create a Socket at the client side. Step 7: Get the host name from the user. Step 8: Establish a connection between the server and the client. Step 9: Transmit the data from the client side to the server. Step 10: Provide the necessary exception handling mechanisms at the client. Step 11: Stop the program. PROGRAM: EchoServer.java import java.net.*; import java.io.*; public class EchoServer { ServerSocket m_ServerSocket; public EchoServer() { try { m_ServerSocket = new ServerSocket(12111); } catch(IOException ioe) { System.out.println("Could not create server socket at 12111. Quitting."); System.exit(-1); } System.out.println("Listening for clients....."); int id = 0; while(true) { try { Socket clientSocket = m_ServerSocket.accept(); ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++); cliThread.start(); } catch(IOException ioe) { System.out.println("Exception encountered on accept. Ignoring. Stack Trace :"); ioe.printStackTrace(); } } } public static void main (String[] args) { new EchoServer(); } class ClientServiceThread extends Thread { Socket m_clientSocket; int m_clientID = -1; boolean m_bRunThread = true; ClientServiceThread(Socket s, int clientID) { m_clientSocket = s;

m_clientID = clientID; } public void run() { BufferedReader in = null; PrintWriter out = null; System.out.println("Accepted Client : ID - " + m_clientID + " : Address " + m_clientSocket.getInetAddress().getHostName()); try { in = new BufferedReader(new InputStreamReader(m_clientSocket.getInputStream())); out = new PrintWriter(new OutputStreamWriter(m_clientSocket.getOutputStream())); while(m_bRunThread) { String clientCommand = in.readLine(); System.out.println("Client Says :" + clientCommand); if(clientCommand.equalsIgnoreCase("quit")) { m_bRunThread = false; System.out.print("Stopping client thread for client : " + m_clientID); } else { out.println(clientCommand); out.flush(); } } } catch(Exception e) { e.printStackTrace(); } finally { try { in.close(); out.close(); m_clientSocket.close(); System.out.println("...Stopped"); } catch(IOException ioe) { ioe.printStackTrace(); } } } } } EchoClient.java import java.net.*; import java.io.*; public class EchoClient { public static void main(String[] args) { if(args.length == 0) { System.out.println("Usage : EchoClient <serverName>"); return; } Socket s = null; try { s = new Socket(args[0], 12111); } catch(UnknownHostException uhe) { System.out.println("Unknown Host :" + args[0]); s = null; } catch(IOException ioe) { System.out.println("Cant connect to server at 12111. Make sure it is running.");

s = null; } if(s == null) System.exit(-1); BufferedReader in = null; PrintWriter out = null; try { in = new BufferedReader(new InputStreamReader(s.getInputStream())); out = new PrintWriter(new OutputStreamWriter(s.getOutputStream())); out.println("Hello"); out.flush(); System.out.println("Server Says : " + in.readLine()); out.println("This"); out.flush(); System.out.println("Server Says : " + in.readLine()); out.println("is"); out.flush(); System.out.println("Server Says : " + in.readLine()); out.println("a"); out.flush(); System.out.println("Server Says : " + in.readLine()); out.println("Test"); out.flush(); System.out.println("Server Says : " + in.readLine()); out.println("Quit"); out.flush(); } catch(IOException ioe) { System.out.println("Exception during communication. Server probably closed connection."); } finally { try { out.close(); in.close(); s.close(); } catch(Exception e) { e.printStackTrace(); } } } } RESULT: Thus the program to develop a multithreaded echo server and client was executed and the output was verified successfully.

S-ar putea să vă placă și