Write a networking program where a Server can access multiple Client & each Client can delete, find, incert, & display the recodes from a mentioned database through the Server.( File must be save as: MultiJDBc.java)
Server Part:-
import java.sql.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
class Data extends Object implements Serializable
{
String rollNumber, name;
int ch;
}
class AppServer implements Runnable
{
ServerSocket server;
Socket client;
Thread serverThread;
int cou=0;
public AppServer(Socket i,String c)
{
client=i;
cou++;
}
public void run()
{
Connect con=new Connect(client);
}
}
class Connect
{
Data data;
ObjectInputStream streamFromClient;
public Connect(Socket fromClient)
{
try
{
streamFromClient=new ObjectInputStream(fromClient.getInputStream());
try
{
data=(Data)streamFromClient.readObject();
}
catch(InvalidClassException e)
{
System.out.println("Data class is Invalid: "+e);
}
catch(NotSerializableException e)
{
System.out.println("The data object is not serializable: "+e);
}
catch(IOException e)
{
System.out.println("Can not read from the client stream: "+e);
}
submit();
}
catch(Exception e)
{
System.out.println("Can not get data from client socket: "+e);
}
}
public void submit()
{
String s="",st;
int i;
st=" Name Roll\n ---- ----\n";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection=DriverManager.getConnection("jdbc:odbc:Student","","");
if(data.ch==3)
{
s=st;
Statement stat=connection.createStatement();
ResultSet rs = stat.executeQuery("SELECT * FROM StudentInfo");
while (rs.next())
{
String a = rs.getString("RollNumber");
String b = rs.getString("Name");
//s=s+"Name: "+b+"\nRoll: "+a+"\n\n";
s=s+" "+b;
for(i=0;i<(20-b.length());i++)
s=s+" ";
s=s+a+"\n";
}
System.out.println(s);
//JOptionPane.showMessageDialog(null,s);
}
else if(data.ch==4)
{
PreparedStatement stat=connection.prepareStatement("SELECT * FROM StudentInfo where RollNumber=?");
stat.setString(1, data.rollNumber);
stat.executeQuery();
ResultSet rs = stat.executeQuery();
while (rs.next())
{
String a = rs.getString("rollNumber");
String b = rs.getString("name");
s="Name: "+b+"\nRoll: "+a;
}
JOptionPane.showMessageDialog(null,s);
}
if(data.ch ==0) {
PreparedStatement stat=connection.prepareStatement("insert into StudentInfo values(?,?)");
stat.setString(1, data.rollNumber);
stat.setString(2, data.name);
stat.executeUpdate();
System.out.println("Data Inserted Successfully !!!");
}
else if(data.ch == 1) {
PreparedStatement stat=connection.prepareStatement("Delete from StudentInfo where RollNumber = ?");
stat.setString(1, data.rollNumber);
stat.executeUpdate();
System.out.println("Data deleted Successfully !!!");
}
else {
PreparedStatement stat=connection.prepareStatement("Update StudentInfo set Name = ? where RollNumber = ?");
stat.setString(1, data.name);
stat.setString(2, data.rollNumber);
stat.executeUpdate();
System.out.println("Data updated Successfully !!!");
}
}
catch(Exception e)
{
System.out.println("Can not process data in the database: "+e);
}
}
}
class MultiJDBc
{
public static void main(String[] args )
{
String st="";
AppServer a;
try
{
int i = 1;
ServerSocket s = new ServerSocket(1001);
for (;;)
{
st=st+i++;
Socket incoming = s.accept( );
a=new AppServer(incoming,st);
Thread t = new Thread(a);
t.start();
i++;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Client Part:-
Save as: MultiClientJDBC.java
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
class Data extends Object implements Serializable
{
String rollNumber, name;
int ch;
}
public class MultiClientJDBC extends JApplet implements ActionListener
{
JPanel panel;
JLabel lblRoll, lblName;
JTextField rollNumber, name;
JButton okButton, cancelButton;
JRadioButton insertRB, deleteRB, updateRB,displayRB,FindRB;
ButtonGroup chGroup;
Data data=new Data();
public void init()
{
panel=new JPanel();
getContentPane().add(panel);
lblRoll=new JLabel("Roll Number:");
lblName=new JLabel("Name:");
rollNumber=new JTextField(15);
panel.add(lblRoll);
panel.add(rollNumber);
name=new JTextField(20);
panel.add(lblName);
panel.add(name);
chGroup = new ButtonGroup();
insertRB = new JRadioButton("Insert");
chGroup.add(insertRB);
insertRB.addActionListener(this);
panel.add(insertRB);
displayRB = new JRadioButton("Display");
chGroup.add(displayRB);
displayRB.addActionListener(this);
panel.add(displayRB);
FindRB = new JRadioButton("Find");
chGroup.add(FindRB);
FindRB.addActionListener(this);
panel.add(FindRB);
deleteRB = new JRadioButton("Delete");
chGroup.add(deleteRB);
deleteRB.addActionListener(this);
panel.add(deleteRB);
updateRB = new JRadioButton("Update");
chGroup.add(updateRB);
updateRB.addActionListener(this);
panel.add(updateRB);
okButton=new JButton(" OK ");
panel.add(okButton);
okButton.addActionListener(this);
cancelButton=new JButton(" Cancel ");
panel.add(cancelButton);
cancelButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource() == insertRB) {
data.ch = 0;
}
else if(evt.getSource() == deleteRB) {
data.ch = 1;
}
else if(evt.getSource() == updateRB) {
data.ch = 2;
}
else if(evt.getSource() ==displayRB) {
data.ch = 3;
}
else if(evt.getSource() ==FindRB) {
data.ch = 4;
}
else if(evt.getSource()==okButton)
{
data.rollNumber=rollNumber.getText();
data.name=name.getText();
if(!insertRB.isSelected() && !deleteRB.isSelected() && !updateRB.isSelected()&& !displayRB.isSelected()&& !FindRB.isSelected()) {
JOptionPane.showMessageDialog(null, "You haven't selected any choice.",
"Selection Error!", JOptionPane.ERROR_MESSAGE);
return;
}
try
{
Socket toServer;
toServer=new Socket(InetAddress.getLocalHost(), 1001);
ObjectOutputStream streamToServer=new ObjectOutputStream(toServer.getOutputStream());
streamToServer.writeObject((Data)data);
streamToServer.close();
}
catch(InvalidClassException e)
{
showStatus("The Data Class is Invalid: "+e);
}
catch(NotSerializableException e)
{
showStatus("The data object is not serializable: "+e);
}
catch(IOException e)
{
showStatus("Can not write to the socket: "+e);
}
chGroup.clearSelection();
}
else if(evt.getSource()==cancelButton)
{
rollNumber.setText("");
name.setText("");
}
}
public static void main(String a[])
{
MultiClientJDBC server=new MultiClientJDBC();
//server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
server.setVisible(true);
server.setSize(450,300);
}
}