Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. package lab3;
  2.  
  3. import java.sql.*;
  4. import java.util.Properties;
  5.  
  6. public class ResultEntity {
  7. private long id;
  8. private Double x;
  9. private Double y;
  10. private Double radius;
  11. private Boolean hit;
  12.  
  13. public long getId() {
  14. return id;
  15. }
  16.  
  17. public void setId(long id) {
  18. this.id = id;
  19. }
  20.  
  21. public Double getX() {
  22. return x;
  23. }
  24.  
  25. public void setX(Double x) {
  26. this.x = x;
  27. }
  28.  
  29. public Double getY() {
  30. return y;
  31. }
  32.  
  33. public void setY(Double y) {
  34. this.y = y;
  35. }
  36.  
  37. public Double getRadius() {
  38. return radius;
  39. }
  40.  
  41. public void setRadius(Double radius) {
  42. this.radius = radius;
  43. }
  44.  
  45. public Boolean getHit() {
  46. return hit;
  47. }
  48.  
  49. public void setHit(Boolean hit) {
  50. this.hit = hit;
  51. }
  52.  
  53. public ResultEntity(Double x, Double y, Double radius, Boolean hit) {
  54. this.x = x;
  55. this.y = y;
  56. this.radius = radius;
  57. this.hit = hit;
  58. }
  59.  
  60. public static void addElem(Double x, Double y, Double radius, boolean hit) throws Exception {
  61.  
  62. Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:orbis", "s225282", "6awqhh");
  63.  
  64. Statement statement = connection.createStatement();
  65.  
  66. String select = "SELECT MAX(ID) FROM RESULT";
  67.  
  68. ResultSet rs = statement.executeQuery(select);
  69.  
  70. int id = 0;
  71.  
  72. while (rs.next()) {
  73. id = rs.getInt(1);
  74. }
  75.  
  76. id++;
  77.  
  78. int int_hit = 0;
  79. if (hit)
  80. int_hit = 1;
  81.  
  82. String insert = "INSERT INTO RESULT (ID, X, Y, RADIUS, HIT) VALUES " +
  83. "(" + id + ", " + x + ", " + y + ", " + radius + ", " + int_hit + ")";
  84.  
  85. statement.execute(insert);
  86. }
  87.  
  88. /**
  89. * @param db строка для соединения с базой в формате JDBC
  90. * @param props параметры для драйвера, такие как пользователь, пароль, порт и тд
  91. * @return соединение с БД
  92. * @throws SQLException
  93. */
  94. public static Connection getConnection(String db, Properties props) throws SQLException {
  95. Connection conn = DriverManager.getConnection(db, props);
  96. System.out.println(conn);
  97. return conn;
  98. }
  99. /**
  100. * @param host адрес хоста с БД
  101. * @param port поорт на котором листенер прослушивает соединение для указанного SID
  102. * @param sid идентификатор схемы БД
  103. * @param user пользоватьель
  104. * @param password и его пароль
  105. * @return параметры для соединения с Oracle DB
  106. */
  107. public static Properties createPropsOracle(String host, String port, String sid, String user, String password) {
  108. Properties props = new Properties();
  109. props.put("host", host);
  110. props.put("port", port);
  111. props.put("sid", sid);
  112. props.put("user", user);
  113. props.put("password", password);
  114. return props;
  115. }
  116.  
  117. /**
  118. * Загружает JDBC драйвер по имени.
  119. * На самом деле это лишнее для всех современных БД. Вызывать loadDriver - НЕ обязательно
  120. *
  121. * @param driverFullName полное имя класса в котором реализован интерфейс вызова Driver для JDBC
  122. * @return загруженный класс
  123. * @throws ClassNotFoundException
  124. */
  125. public static Class loadDriver(String driverFullName) throws ClassNotFoundException {
  126. Class c = Class.forName(driverFullName);
  127. System.out.println(c);
  128. return c;
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement