I have created the client and server pages. I need to connected to a database, but when I run the server first it is working. Starting the Server then I run the client page, I got the error was display SEVERE: null. I don't why. I have attached code below. I have a problem with client page. Is there any thing wrong? What I tried I attached below. Give the solution for it, thanks.
publisher.java
public class publisher {
String id;
String pname;
String price;
String qty;
public static void main(String args[])
{
new publisher();
}
}
Server.java
import java.io.*;
import java.sql.*;
import java.net.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
public class Server extends Thread {
Statement stmt=null;
Vector records = new Vector(10,10);
ResultSet rs = null;
ServerSocket server = null;
Socket client = null;
Connection con = null;
ObjectOutputStream out =null;
String str = null;
publisher pub = null;
public Server()
{
try {
server = new ServerSocket(1700);
System.out.println("Starting the Server");
start();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void run()
{
while(true)
{
try {
int CC;
client = server.accept();
System.out.println("Connection accepted");
out = new ObjectOutputStream(client.getOutputStream());
System.out.println("OutputStream received");
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/smobile", "root","");
stmt = con.createStatement();
rs = stmt.executeQuery("select * from products");
records.removeAllElements();
ResultSetMetaData RSMD = rs.getMetaData();
CC = RSMD.getColumnCount();
while(rs.next())
{
pub = new publisher();
pub.id = rs.getString(1);
pub.pname = rs.getString(2);
pub.price = rs.getString(3);
pub.qty = rs.getString(4);
records.addElement(pub);
System.out.println("row returned");
}
out.writeObject(records);
out.close();
System.out.println("String returned");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String args[])
{
new Server();
}
Client.java
String str = "";
ResultSet rs;
Vector records;
publisher pub = null;
int a = 0;
ObjectInputStream br = null;
Connection con;
PreparedStatement pst;
private void table_update()
{
int i = 0;
Socket socket1;
try {
socket1 = new Socket( "192.168.225.1", 1700);
System.out.println("client is now connected with server");
br = new ObjectInputStream(socket1.getInputStream());
records = (Vector)br.readObject();
DefaultTableModel DFT = (DefaultTableModel) jTable1.getModel();
DFT.setRowCount(0);
while (i < records.size()) {
Vector v2 = new Vector();
pub = (publisher)records.elementAt(i);
v2.add(pub.id);
v2.add(pub.pname);
v2.add(pub.price);
v2.add(pub.qty);
i++;
DFT.addRow(v2);
}
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
Error displayed
Client table_update
SEVERE: null
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:3014)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1575)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:464)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
at Client.table_update(Client.java:62)
at Client.<init>(Client.java:22)