Я искал в Интернете и нашел несколько вопросов от людей, сталкивающихся с той же проблемой, но решения не помогли мне, теперь моя проблема заключается в том, что я создал сервер Java, который должен получать сообщения от клиента, который имеет несколько классов, но он получает только одно сообщение от каждого класса и затем не получает ничего от того же класса, то же самое происходит, когда я отправляю что-либо из любого другого класса
вот код:
package Database;
import Clinic.QueriesController;
import Clinic.ReciptionController;
import javafx.application.Platform;
import java.io.*;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.SocketException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Networking {
private ServerSocket ss;
private QueriesController qc;
private DBConnection db;
{
try {
qc = new QueriesController();
db = new DBConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
// getCon() is a method I declared in the class DBConnection to simply get connection to the data base
private Connection con = db.getCon();
public void networkConnection() {
new Thread(() -> {
try {
ss = new ServerSocket(16000);
Platform.runLater(() ->
System.out.println("Server started"));
while (true){
// the following method is declared below
handleData();
}
} catch(SocketException e){
System.out.println("Client disconnected");
} catch(Exception e){
e.printStackTrace();
}
}).start();
}
public void handleData() throws IOException{
Socket s = ss.accept();
BufferedReader inputFromClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter outputToClient = new PrintWriter(s.getOutputStream(), true);
// messages received from the client contain the command and the information of the message separated by ";"
// and the content of the message are separated by "," and every thing gets split in the following code
String messageFromClient = inputFromClient.readLine(); // receive the message
//check the command gained from client
System.out.println("Received Code: " + messageFromClient);
if (messageFromClient != null) {
// first split to get the command
String[] commandInfo = messageFromClient.split(";");
String[] messageContent;
PreparedStatement ps2;
ResultSet rs;
PreparedStatement ps;
//the switch statement takes the first part of the message received and each case splits the second part of the message based on the ","
switch (commandInfo[0]) {
case "insertMS":
messageContent = commandInfo[1].split(",");
try {
ps = con.prepareStatement("insert into `patiantInfo` values(null,?,?,?,?,?);");
ps.setString(1, messageContent[0]);
ps.setString(2, messageContent[1]);
ps.setString(3, messageContent[2]);
ps.setString(4, messageContent[3]);
ps.setString(5, messageContent[4]);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
break;
case "deleteMS":
try {
ps = con.prepareStatement("delete from patiantInfo where id = ?");
ps.setString(1, commandInfo[1]);
qc.ps = con.prepareStatement("delete from visitInfo where patiantID = ?");
qc.ps.setString(1, commandInfo[1]);
ps.executeUpdate();
qc.ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
break;
case "editMS":
try {
messageContent = commandInfo[1].split(",");
ps = con.prepareStatement("update patiantInfo set name = ?," +
" age = ?," +
" sex = ?," +
" address = ?," +
" connect = ?" +
" where id = ?");
ps.setString(1, messageContent[0]);
ps.setString(2, messageContent[1]);
ps.setString(3, messageContent[2]);
ps.setString(4, messageContent[3]);
ps.setString(5, messageContent[4]);
ps.setString(6, messageContent[5]);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
break;
case "insertMSF1":
try {
messageContent = commandInfo[1].split(",");
qc.ps = qc.con.prepareStatement("insert into visitInfo values (null,?,?,?,?,null,null,null,null,null,null,null) ");
qc.ps.setString(1, messageContent[0]);
qc.ps.setString(2, messageContent[1]);
qc.ps.setString(3, messageContent[2]);
qc.ps.setString(4, messageContent[3]);
qc.ps.executeUpdate();
ps2 = con.prepareStatement("select num from visitCount");
rs = ps2.executeQuery();
int num;
while (rs.next()) {
num = rs.getInt("num");
num++;
ps = con.prepareStatement("update visitCount set num = ?");
ps.setString(1, String.valueOf(num));
ps.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
break;
case "updateMSF2":
try {
messageContent = commandInfo[1].split(",");
qc.ps = qc.con.prepareStatement("update visitInfo set attend_date = ?," +
" attend_time = ?," +
" visit_type = ?," +
" attend = ?," +
" attend_type = ?" +
" where patiantID = ? and id = ? ");
ps2 = con.prepareStatement("select num from visitCount");
rs = ps2.executeQuery();
qc.ps.setString(1, messageContent[0]);
qc.ps.setString(2, messageContent[1]);
qc.ps.setString(3, messageContent[2]);
qc.ps.setString(4, messageContent[3]);
qc.ps.setString(5, messageContent[4]);
qc.ps.setString(6, messageContent[5]);
while (rs.next()) {
qc.ps.setString(7, rs.getString("num"));
}
qc.ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
break;
case "updateMSF3":
try {
messageContent = commandInfo[1].split(",");
qc.ps = qc.con.prepareStatement("update visitInfo set payment_date = ?," +
" payment_value = ?" +
" where patiantID = ? and id = ? ");
ps2 = con.prepareStatement("select num from visitCount");
rs = ps2.executeQuery();
qc.ps.setString(1, messageContent[0]);
qc.ps.setString(2, messageContent[1]);
qc.ps.setString(3, messageContent[2]);
while (rs.next()) {
qc.ps.setString(4, rs.getString("num"));
}
qc.ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
break;
case "deleteES":
try {
qc.ps = con.prepareStatement("delete from visitInfo where id = ? ");
qc.ps.setString(1, commandInfo[1]);
qc.ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
break;
}
}
}
}
Я точно знаю, что он не получает сообщения, потому что он не печатает содержание сообщений и не попадает в оператор switch