当前位置: 技术问答>java相关
一个很普通的问题,关于C/S的一个socket,编译可通过,就是连不上,麻烦各位看一下,急!!!
来源: 互联网 发布时间:2015-10-27
本文导语: 基本思想是这样的,点击客户端的一个按钮可以通过服务器端连接数据库进行查询,并将结果显示在客户端的界面上。有一个双向的过程,客户端要发送数据(一个类),也要接收数据,服务器端也要接收数据处理好...
基本思想是这样的,点击客户端的一个按钮可以通过服务器端连接数据库进行查询,并将结果显示在客户端的界面上。有一个双向的过程,客户端要发送数据(一个类),也要接收数据,服务器端也要接收数据处理好后再发回客户端。代码可以运行,但查询时服务器端抛出异常,说error by Connectjava.lang.ClassNotFoundException: hrclient.HREmployeeInfo
客户端:
//要查询的流
public class HREmployeeInfo extends Object implements java.io.Serializable
{
int employeeCode;
String employeeName;
String employeeAge;
String employeeSex;
String employeeAddress;
}
void queryButton_actionPerformed(ActionEvent e) {
HREmployeeInfo data = new HREmployeeInfo();
HREmployeeInfo data1 = new HREmployeeInfo();
data.employeeCode =Integer.parseInt(EmployeeCodeTextField.getText());
try
{
Socket toServer;
toServer = new Socket("localhost", 6666);
ObjectOutputStream streamToServer = new ObjectOutputStream(toServer.getOutputStream());
streamToServer.writeObject(data);
ObjectInputStream streamFromServer = new ObjectInputStream(toServer.getInputStream());
data1 =(HREmployeeInfo)streamFromServer.readObject();
EmployeeNameTextField.setText(data1.employeeName);
EmployeeAgeTextField.setText(data1.employeeAge);
EmployeeSexTextField.setText(data1.employeeSex);
EmployeeAddressTextField.setText(data1.employeeAddress);
streamFromServer.close();
streamToServer.close();
}
catch(Exception ea)
{
System.out.println("error by query" + ea);
}
}
服务器端:
……
try{
server = new ServerSocket(6666);
serverThread = new Thread(this);
serverThread.start();
}
catch(Exception e)
{
System.out.println("Cannot start the thread" + e);
}
……
public void run()
{
try
{
while(true)
{
fromClient = server.accept();
Connect con = new Connect(fromClient);
}
}
catch(Exception ex5)
{
System.out.println("Cannot listen to the Client" + ex5);
}
}
……
public class Connect
{
HREmployeeInfo data;
ObjectInputStream streamFromClient;
ObjectOutputStream streamToClient;
public Connect(Socket inFromClient)
{
try
{
streamFromClient = new ObjectInputStream(inFromClient.getInputStream());
data = (HREmployeeInfo)streamFromClient.readObject();
HREmployeeInfo data1 = new HREmployeeInfo();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn = DriverManager.getConnection("jdbc:odbc:data", "sa", "");
PreparedStatement stat = cn.prepareStatement("Select * from EmployeeInformation where employeeCode=?");
stat.setInt(1,data.employeeCode);
ResultSet result = stat.executeQuery();
result.next();
data1.employeeName = result.getString(2);
data1.employeeAge = result.getString(3);
data1.employeeSex = result.getString(4);
data1.employeeAddress = result.getString(6);
streamToClient =(ObjectOutputStream)inFromClient.getOutputStream();
streamToClient.writeObject(data1);
cn.close();
streamFromClient.close();
streamToClient.close();
}
catch(Exception ex6)
{
System.out.println("error by Connect" + ex6);
}
}
}
客户端:
//要查询的流
public class HREmployeeInfo extends Object implements java.io.Serializable
{
int employeeCode;
String employeeName;
String employeeAge;
String employeeSex;
String employeeAddress;
}
void queryButton_actionPerformed(ActionEvent e) {
HREmployeeInfo data = new HREmployeeInfo();
HREmployeeInfo data1 = new HREmployeeInfo();
data.employeeCode =Integer.parseInt(EmployeeCodeTextField.getText());
try
{
Socket toServer;
toServer = new Socket("localhost", 6666);
ObjectOutputStream streamToServer = new ObjectOutputStream(toServer.getOutputStream());
streamToServer.writeObject(data);
ObjectInputStream streamFromServer = new ObjectInputStream(toServer.getInputStream());
data1 =(HREmployeeInfo)streamFromServer.readObject();
EmployeeNameTextField.setText(data1.employeeName);
EmployeeAgeTextField.setText(data1.employeeAge);
EmployeeSexTextField.setText(data1.employeeSex);
EmployeeAddressTextField.setText(data1.employeeAddress);
streamFromServer.close();
streamToServer.close();
}
catch(Exception ea)
{
System.out.println("error by query" + ea);
}
}
服务器端:
……
try{
server = new ServerSocket(6666);
serverThread = new Thread(this);
serverThread.start();
}
catch(Exception e)
{
System.out.println("Cannot start the thread" + e);
}
……
public void run()
{
try
{
while(true)
{
fromClient = server.accept();
Connect con = new Connect(fromClient);
}
}
catch(Exception ex5)
{
System.out.println("Cannot listen to the Client" + ex5);
}
}
……
public class Connect
{
HREmployeeInfo data;
ObjectInputStream streamFromClient;
ObjectOutputStream streamToClient;
public Connect(Socket inFromClient)
{
try
{
streamFromClient = new ObjectInputStream(inFromClient.getInputStream());
data = (HREmployeeInfo)streamFromClient.readObject();
HREmployeeInfo data1 = new HREmployeeInfo();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn = DriverManager.getConnection("jdbc:odbc:data", "sa", "");
PreparedStatement stat = cn.prepareStatement("Select * from EmployeeInformation where employeeCode=?");
stat.setInt(1,data.employeeCode);
ResultSet result = stat.executeQuery();
result.next();
data1.employeeName = result.getString(2);
data1.employeeAge = result.getString(3);
data1.employeeSex = result.getString(4);
data1.employeeAddress = result.getString(6);
streamToClient =(ObjectOutputStream)inFromClient.getOutputStream();
streamToClient.writeObject(data1);
cn.close();
streamFromClient.close();
streamToClient.close();
}
catch(Exception ex6)
{
System.out.println("error by Connect" + ex6);
}
}
}
|
你调试一下,错误是在哪儿发生的。
我觉得应该是
找不到类:HREmployeeInfo
我觉得应该是
找不到类:HREmployeeInfo