我正在尝试使用ObjectInputStream / ObjectOutputStream通过套接字从服务器向客户端发送ArrayList,但是我收到的对象发送的错误是null。 应该发生以下情况(但它不会): 1.在服务器端,正在从MySQL数据库填充ArrayList 2.发送给客户端并用于填充jTable。
连接文件(服务器端)我相信我的问题是我的void run()......
package networkclientserver;
import java.net.*;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
class ConnectionCW implements Runnable
{
private Socket s;
private ObjectOutputStream ooos;
private ObjectInputStream oois;
ResultSet rs;
ConnectionCW(Socket s)
{
this.s = s;
}
public void run()
{
try
{
ObjectOutputStream ooos = new ObjectOutputStream(this.s.getOutputStream());
ObjectInputStream oois = new ObjectInputStream(this.s.getInputStream());
this.getTopology();
this.ooos.writeObject(getTopology());
this.ooos.flush();
} catch (Exception ex)
{
Logger.getLogger(ConnectionCW.class.getName()).log(Level.SEVERE, null, ex);
}
} // end of method run*/
}
public ArrayList<ReadNet> getTopology() throws Exception
{
ArrayList<ReadNet> getTopology2 = new ArrayList<ReadNet>();
Connection conn2 = getConnection();
String query = "SELECT * FROM datatopology ";
Statement st;
st=conn2.createStatement();
rs=st.executeQuery(query);
ReadNet readNet;
while(rs.next())
{
readNet = new ReadNet(rs.getInt("id"),rs.getInt("numberNodes"),rs.getInt("numberHubs"),rs.getInt("numberSwitches"),rs.getString("topologyStructure"),rs.getString("country"),rs.getString("status"));
getTopology2.add(readNet);
}
return getTopology2;
}
public Connection getConnection() throws Exception
{
try
{
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/networkstopology";
String username = "user";
String password = "pass";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
System.out.println("Connected to MySQL!");
return conn;
} catch (Exception e)
{
System.out.println(e);
}
return null;
}
}
客户端(按下按钮时运行)
private void buttonReadActionPerformed(java.awt.event.ActionEvent evt)
{
try
{
ArrayList<ReadNet> setTopology = (ArrayList<ReadNet>) ios.readObject();
DefaultTableModel model = (DefaultTableModel) topologyTable1.getModel();
Object[] row = new Object[7];
for (int i = 0; i < setTopology.size(); i++)
{
row[0] = setTopology.get(i).getId();
row[1] = setTopology.get(i).getNNodes();
row[2] = setTopology.get(i).getNHubs();
row[3] = setTopology.get(i).getNSwitches();
row[4] = setTopology.get(i).getCountry();
row[5] = setTopology.get(i).getTopology();
row[6] = setTopology.get(i).getStatus();
model.addRow(row);
}
} catch (IOException ex)
{
Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex)
{
Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
ReadNet课程
package networkclientserver;
import java.io.Serializable;
class ReadNet implements Serializable
{
int id;
int nnodes;
int nhubs;
int nswitch;
String topology;
String country;
String status;
ReadNet(int id,int nodes, int hubs, int switches, String countries, String topology, String status)
{
this.id=id;
this.nnodes = nodes;
this.nhubs = hubs;
this.nswitch = switches;
this.country = countries;
this.topology = topology;
this.status = status;
}
public int getId()
{
return this.id;
}
public int getNNodes()
{
return this.nnodes;
}
public int getNHubs()
{
return this.nhubs;
}
public int getNSwitches()
{
return this.nswitch;
}
String getTopology()
{
return this.topology;
}
String getCountry()
{
return this.country;
}
String getStatus()
{
return this.status;
}
}