Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 */ }
- }
- }
- }
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement