JDBC in Network programming.( File must be save as: AppServerJDBC.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;
}
public class AppServer implements Runnable
{
ServerSocket server;
Socket client;
Thread serverThread;
public AppServer()
{
try
{
server=new ServerSocket(1001);
serverThread=new Thread(this);
serverThread.start();
System.out.println("Server has been started");
System.out.println("-----------------------");
}
catch(Exception e)
{
System.out.println("Can not start the server: "+e);
}
}
public void run()
{
try
{
while(true)
{
client=server.accept();
Connect con=new Connect(client);
}
}
catch(Exception e)
{
System.out.println("Can not get data from client: "+e);
}
}
public static void main(String args[])
{
new AppServer();
}
}
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="";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection=DriverManager.getConnection("jdbc:odbc:Student","","");
if(data.ch==3)
{
s="";
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";
}
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);
}
}
}
/*
CLIENT PART:
*/
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 ClientNeedJDBC 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;
}
/*if(!deleteRB.isSelected() && (data.rollNumber.equals("") || data.name.equals(""))) {
JOptionPane.showMessageDialog(null, "You haven't entered all the values required.",
"Data 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[])
{
ClientNeedJDBC server=new ClientNeedJDBC();
//server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
server.setVisible(true);
server.setSize(450,300);
}
}