MySQL服务端读取客户端_通过服务器类从mysql读取到客户端

这篇博客讨论了如何实现一个服务器程序,该程序从MySQL数据库读取数据并将其发送到客户端。作者遇到的问题是,当从数据库中获取信息(如drinktable的Name列)并尝试将数据发送到客户端时,客户端无法正确接收。然而,当发送简单的字符串时,通信正常。代码示例包括使用ServerSocket、DataOutputStream和ObjectOutputStream在Java中建立服务器,并使用DataInputStream和ObjectInputStream在客户端接收数据。
摘要由CSDN通过智能技术生成

所以基本上,我试图做的是通过我创建的服务器从我的数据库中读取MySQL。我把我的数据库中的信息放入ArrayList中,但客户端无法读取它。当我添加简单的字符串时,它可以工作。但不是当它从我的数据库。任何解决方案

public class Server implements Runnable {

private ServerSocket server;

private Socket socket;

private ObjectOutputStream oos = null;

public Server() {

try {

server = new ServerSocket(4444);

new Thread(this).start();

} catch (Exception e) {

System.out.println(e);

}

}

public void run() {

System.out.println("Server running");

while (true) {

try {

socket = server.accept();

sayHi();

System.out.println("Hallå");

} catch (Exception e) {

}

}

}

private void sayHi(){

DataOutputStream dos;

try {

dos = new DataOutputStream(socket.getOutputStream());

oos = new ObjectOutputStream(socket.getOutputStream());

dos.writeUTF("Hej");

sendNames();

} catch (IOException ex) {

Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);

}

}

public void sendNames() {

ArrayList drinkar = new ArrayList();

Connection con = null;

String url = "jdbc:mysql://localhost:3306/";

String db = "drycker";

String driver = "com.mysql.jdbc.Driver";

String user = "root";

String pass = "";

try{

Class.forName(driver).newInstance();

con = DriverManager.getConnection(url+db, user, pass);

try{

Statement st = (Statement) con.createStatement();

ResultSet res = st.executeQuery("SELECT * FROM drinktable");

while (res.next()) {

String s = res.getString("Name");

for(int i = 0; i

drinkar.add(s);

}

}

con.close();

}

catch (SQLException s){

System.out.println("SQL code does not execute.");

}

}

catch (Exception e){

e.printStackTrace();

}

ArrayList a = new ArrayList();

a.add("Hejsan");

a.add("Svejsan");

try {

FileOutputStream fos = new FileOutputStream("randomList");

oos = new ObjectOutputStream(fos);

oos.writeObject(drinkar);

oos.flush();

oos.reset();

oos.close();

fos.close();

} catch (Exception e) {

}

}

public static void main(String[] args) {

new Server();

}

}

public class Client implements Runnable {

private Socket socket;

private DataInputStream dis;

private ObjectInputStream ois = null;

public Client() {

socket = new Socket();

InetSocketAddress ipPort = new InetSocketAddress("192.168.0.10", 4444);

try {

socket.connect(ipPort);

dis = new DataInputStream(socket.getInputStream());

ois = new ObjectInputStream(socket.getInputStream());

} catch (Exception e) {

}

new Thread(this).start();

}

public void run() {

while (true) {

try {

String msg = dis.readUTF();

if (msg.equals("Hej")) {

Thread.sleep(50);

receiveArrayList();

}

} catch (Exception e) {

}

}

}

public void receiveArrayList() {

try {

FileInputStream fis = new FileInputStream("randomList");

ois = new ObjectInputStream(fis);

ArrayList a= (ArrayList) (ois.readObject());

for(int i = 0; i < a.size(); i++){

System.out.println((String)a.get(i));

}

ois.close();

} catch (ClassNotFoundException ex) {

System.out.println(ex);

} catch (IOException ex) {

System.out.println(ex);

}

}

public static void main(String[] args) {

new Client();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值