我遇到以下Threaded Client Server程序的问题。所有它应该做的是从文本字段和按下按钮获取值以将数据保存到SQL db表。虽然我构建了所有内容,但它们都已成功构建,但ClientGUI(swing)将无法打开。我相信我的问题在于ConnectionCW类,但无法弄清楚我应该在void run
中做些什么。您能否看一下如何更改ConnectionCW以正常工作?提前致谢!代码在这里:
Servercode:
package networkclientserver;
import java.io.*;
import java.util.*;
import java.net.*;
public class Server
{
public static void main(String[] args) throws Exception
{
int connectionCount = 0; // Count of clients connecting
Thread connThread;
System.out.println("Server starting");
try
{
ServerSocket ss = new ServerSocket(2000);
while (true)
{
Socket s = ss.accept();
// Network n;
connectionCount++;
System.out.println("Connection " + connectionCount + " made");
//Create and start thread to process client requests
connThread = new Thread(new ConnectionCW(s)
{
});
connThread.start();
}
} catch (IOException e)
{
System.out.println("Trouble making a connection" + e);
}
}
}
ClientGUI:
package networkclientserver;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import javax.swing.JComponent;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ClientGUI extends javax.swing.JFrame
{
ObjectInputStream ios = null;
ObjectOutputStream oos = null;
Socket s;
public ClientGUI()
{
initComponents();
try
{
this.s = new Socket("127.0.0.1", 2000);
this.ios = new ObjectInputStream(s.getInputStream());
this.oos = new ObjectOutputStream(s.getOutputStream());
} catch (IOException e)
{
System.out.println("Error connecting with the Server " + e);
}
}
public static void main(String args[])
{
ClientGUI gui = new ClientGUI();
gui.setVisible(true);
}
}
ConnectionCW:
package networkclientserver;
import java.io.*;
import java.util.*;
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.util.logging.Level;
import java.util.logging.Logger;
class ConnectionCW implements Runnable
{
private Socket s;
ConnectionCW(Socket s)
{
this.s = s;
}
public void run()
{
try
{
this.insertIntoTable();
} catch (Exception ex)
{
Logger.getLogger(ConnectionCW.class.getName()).log(Level.SEVERE, null, ex);
}
} // end of method run*/
public void insertIntoTable() throws Exception
{
ObjectInputStream oois = new ObjectInputStream(this.s.getInputStream());
Network netW = (Network) oois.readObject();
try
{
Connection conn = getConnection();
PreparedStatement create = conn.prepareStatement("INSERT INTO datatopology (numberNodes,numberHubs,numberSwitches,topologyStructure,country,status) VALUES ('" + netW.getNNodes() + "','" + netW.getNHubs() + "','" + netW.getNSwitches() + "','" + netW.getTopology() + "','" + netW.getCountry() + "','" + netW.getStatus() + "')");
create.executeUpdate();
oois.close();
} catch (Exception e)
{
System.out.println(e);
} finally
{
System.out.println("Function complete!");
}
}
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;
}
}