Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. public class DBEngine {
  2.  
  3. public static final String url = "jdbc:mysql://localhost:3306/mydbtest";
  4. public static final String user = "root";
  5. public static final String password = "root";
  6.  
  7.  
  8. public static Connection con;
  9. public static Statement stmt;
  10. public static ResultSet rs;
  11.  
  12.  
  13. public static void main(String args[]) {
  14. String query = "select id, name, age, email from users";
  15.  
  16. try {
  17. // opening database connection to MySQL server
  18. con = DriverManager.getConnection(url, user, password);
  19.  
  20. // getting Statement object to execute query
  21. stmt = con.createStatement();
  22.  
  23. // executing SELECT query
  24. rs = stmt.executeQuery("select id, name, age, email from users");
  25.  
  26. while (rs.next()) {
  27. int id = rs.getInt("id");
  28. String name = rs.getString("name");
  29. int age = rs.getInt("age");
  30. String email = rs.getString("email");
  31. String all = ("Пользователь " + name + " , которому " + age + "год(а)/лет, имеет ID " + id + " и email " + email);
  32. System.out.println(all);
  33. GUI.area.append(all);
  34. }
  35.  
  36. } catch (SQLException sqlEx) {
  37. sqlEx.printStackTrace();
  38. } finally {
  39. //close connection ,stmt and resultset here
  40. try { con.close(); } catch(SQLException se) { /*can't do anything */ }
  41. try { stmt.close(); } catch(SQLException se) { /*can't do anything */ }
  42. try { rs.close(); } catch(SQLException se) { /*can't do anything */ }
  43. }
  44. }
  45.  
  46. }
  47.  
  48. public class Server {
  49.  
  50. public static final String url = "jdbc:mysql://localhost:3306/mydbtest";
  51. public static final String user = "root";
  52. public static final String password = "root";
  53.  
  54. // JDBC variables for opening and managing connection
  55. public static Connection con;
  56. public static Statement stmt;
  57. public static ResultSet rs;
  58. private static ServerSocket server;
  59. private static int port = 9876;
  60.  
  61.  
  62. public static void main(String args[]) throws IOException, ClassNotFoundException{
  63. server = new ServerSocket(port);
  64. DBEngine show1DB = new DBEngine();
  65. while(true){
  66. System.out.println("Ожидаем клиента");
  67. Socket socket = server.accept();
  68. ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
  69. String message = (String) ois.readObject();
  70. int dlina = message.length();
  71. System.out.println("Получено сообщение: " + message);
  72. ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
  73. oos.writeObject(dlina+" ");
  74. ois.close();
  75. oos.close();
  76. socket.close();
  77. if(message.equalsIgnoreCase("exit")) break;
  78. if(message.startsWith("*"))
  79. show1DB.main(args);
  80.  
  81.  
  82.  
  83. }
  84. System.out.println("Сервер отключен");
  85. server.close();
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement