Please do not judge strictly, I'm still quite new to Java. Perhaps my question from the category of "but is tohever veSimple". There is a graphical interface, client, server and database in MySQL. The client picks up a string from the Graphical Interface TextArea, sends it to the server, inserting the prefix in the form *. The server seeing * at the beginning of the accepted line should connect to the database and display the table in the TextArea of ​​the graphical interface. Implemented everything like this + wrote a DBEngine class that performs all actions from the database.

public class DBEngine { public static final String url = "jdbc:mysql://localhost:3306/mydbtest"; public static final String user = "root"; public static final String password = "root"; public static Connection con; public static Statement stmt; public static ResultSet rs; public static void main(String args[]) { String query = "select id, name, age, email from users"; try { // opening database connection to MySQL server con = DriverManager.getConnection(url, user, password); // getting Statement object to execute query stmt = con.createStatement(); // executing SELECT query rs = stmt.executeQuery("select id, name, age, email from users"); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); String email = rs.getString("email"); String all = ("Пользователь " + name + " , которому " + age + "год(а)/лет, имеет ID " + id + " и email " + email); System.out.println(all); GUI.area.append(all); } } catch (SQLException sqlEx) { sqlEx.printStackTrace(); } finally { //close connection ,stmt and resultset here try { con.close(); } catch(SQLException se) { /*can't do anything */ } try { stmt.close(); } catch(SQLException se) { /*can't do anything */ } try { rs.close(); } catch(SQLException se) { /*can't do anything */ } } } } 

And with a separate launch of DBEngine, everything is correctly displayed in the console. Here is the server:

 public class Server { public static final String url = "jdbc:mysql://localhost:3306/mydbtest"; public static final String user = "root"; public static final String password = "root"; // JDBC variables for opening and managing connection public static Connection con; public static Statement stmt; public static ResultSet rs; private static ServerSocket server; private static int port = 9876; public static void main(String args[]) throws IOException, ClassNotFoundException{ server = new ServerSocket(port); DBEngine show1DB = new DBEngine(); while(true){ System.out.println("Ожидаем клиента"); Socket socket = server.accept(); ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); String message = (String) ois.readObject(); int dlina = message.length(); System.out.println("Получено сообщение: " + message); ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject(dlina+" "); ois.close(); oos.close(); socket.close(); if(message.equalsIgnoreCase("exit")) break; if(message.startsWith("*")) show1DB.main(args); } System.out.println("Сервер отключен"); server.close(); } } 

But, actually, the error on the server itself enter image description here

Thank you, if you read to here)

  • 2
    Probably does not find jdbc driver. There is such a thing in java, the classpath is called. And this java finds only those classes that are in this classpath. It is necessary to add the driver in the classpath or classpath correct, to point to the driver. - Sergey
  • Well, or, if you have an old MYSQL driver, then you need to Class.forName() with it, which is not in your code. - enzo

0