Advertisement
Guest User

Untitled

a guest
Jan 25th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.78 KB | None | 0 0
  1. import com.mysql.fabric.jdbc.FabricMySQLDriver;
  2.  
  3. import java.sql.*;
  4.  
  5. public class Main {
  6.  
  7.     static String QUERY_DB_URL = "jdbc:mysql://localhost:3306/new_schema2";
  8.     static String USERNAME = "root";
  9.     static String PASSWORD = "root";
  10.  
  11.  
  12.     // Моделька
  13.     public static class UserQuery {
  14.         private Long date;
  15.         private Integer request_id;
  16.         private Integer user_id;
  17.         private String user_name;
  18.         private  String platform;
  19.         private String query;
  20.         private Integer query_type;
  21.                
  22.         public UserQuery() {
  23.         }
  24.         public UserQuery(Long date,
  25.                             Integer request_id,
  26.                             Integer user_id,
  27.                             String  user_name,
  28.                             String  platform,
  29.                             String  query,
  30.                             Integer query_type)
  31.         {
  32.             this.request_id = request_id;
  33.             this.user_id = user_id;
  34.             this.user_name = user_name;
  35.             this.platform = platform;
  36.             this.query = query;
  37.             this.query_type = query_type;
  38.         }
  39.  
  40.         public Long getDate() {
  41.             return date;
  42.         }
  43.         public void setDate(Long date) {
  44.             this.date = date;
  45.         }
  46.  
  47.         public Integer getRequest_id() {
  48.             return request_id;
  49.         }
  50.         public void setRequest_id(Integer request_id) {
  51.             this.request_id = request_id;
  52.         }
  53.  
  54.         public Integer getUser_id() {
  55.             return user_id;
  56.         }
  57.         public void setUser_id(Integer user_id) {
  58.             this.user_id = user_id;
  59.         }
  60.  
  61.         public String getUser_name() {
  62.             return user_name;
  63.         }
  64.         public void setUser_name(String user_name) {
  65.             this.user_name = user_name;
  66.         }
  67.  
  68.         public String getPlatform() {
  69.             return platform;
  70.         }
  71.         public void setPlatform(String platform) {
  72.             this.platform = platform;
  73.         }
  74.  
  75.         public String getQuery() {
  76.             return query;
  77.         }
  78.         public void setQuery(String query) {
  79.             this.query = query;
  80.         }
  81.  
  82.         public Integer getQuery_type() {
  83.             return query_type;
  84.         }
  85.         public void setQuery_type(Integer query_type) {
  86.             this.query_type = query_type;
  87.         }
  88.  
  89.     }
  90.  
  91.  
  92.     // Пример запросов
  93.     public static UserQuery getUserQuery(int id, Connection connection) {
  94.         try {
  95.             Statement stmt = connection.createStatement();
  96.             ResultSet rs = stmt.executeQuery("SELECT * FROM user WHERE request_id=" + id);
  97.             if(rs.next())
  98.             {
  99.                 UserQuery uq = new UserQuery();
  100.                 // как-то с найденной записью работать вроде такого
  101.                 uq.setQuery(uq.getQuery() + " | отправлено с " + uq.getQuery_type());
  102.                 return uq;
  103.             }
  104.         } catch (SQLException ex) {
  105.             ex.printStackTrace();
  106.         }
  107.         return null;
  108.     }
  109.  
  110.  
  111.     // Начальная инициализация
  112.     public static void main(String[] args) {
  113.  
  114.         try {
  115.             Driver driver = new FabricMySQLDriver();
  116.             DriverManager.registerDriver(driver);
  117.         } catch (SQLException e) {
  118.             System.err.print("Не удалось загрузить класс драйвера MySQL\n");
  119.             e.printStackTrace();
  120.         }
  121.  
  122.         try (Connection connection = DriverManager.getConnection(QUERY_DB_URL, USERNAME, PASSWORD)) {
  123.  
  124.             getUserQuery(1, connection);
  125.  
  126.         } catch (SQLException e) {
  127.             e.printStackTrace();
  128.         }
  129.     }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement