Advertisement
Guest User

Untitled

a guest
Nov 12th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.ResultSetMetaData;
  3. import java.sql.Statement;
  4. import java.util.*;
  5. import java.sql.*;
  6.  
  7. public class DataBase
  8. {
  9. String url = "jdbc:mysql://localhost:3306/";
  10. String user = "root";
  11. String password = "Qazxsw2!";
  12.  
  13.  
  14. /* *//**
  15. * Adds film to the data base
  16. *
  17. * @param f Object of Film class
  18. */
  19. public void addFilm(Object ob)
  20. {
  21. try {
  22. Class.forName("com.mysql.jdbc.Driver");
  23. Connection conn = null;
  24. java.sql.PreparedStatement pst = null;
  25. conn = DriverManager.getConnection(url, user, password);
  26. if (conn != null) System.out.println("Connected");
  27. String sql = "INSERT INTO sakila.films (Title,Rating,RatingNumber) VALUES(?,?,?)";
  28. pst = conn.prepareStatement(sql);
  29.  
  30. /*
  31. pst.setString(1, f.title);
  32. pst.setFloat(2, f.rating);
  33. pst.setInt(3, f.ratingNumber);
  34. */
  35. pst.executeUpdate();
  36.  
  37. } catch (ClassNotFoundException e) {
  38. System.out.println("Driver problem");
  39. } catch (SQLException e) {
  40. System.out.println(e);
  41. }
  42. }
  43.  
  44.  
  45.  
  46. public void dodajKlase()
  47. {
  48. try {
  49. Class.forName("com.mysql.jdbc.Driver");
  50. Connection conn = null;
  51. java.sql.PreparedStatement pst = null;
  52. conn = DriverManager.getConnection(url, user, password);
  53. if (conn != null) System.out.println("Connected");
  54. String sql = "INSERT INTO generator_postaci_rpg1.klasa (ID_KL, Nazwa_KL, Poziom_KL) VALUES(?,?,?)";
  55. pst = conn.prepareStatement(sql);
  56.  
  57.  
  58. pst.setInt(1, 1);
  59. pst.setString(2, "Wojownik");
  60. pst.setInt(3, 5);
  61.  
  62. pst.executeUpdate();
  63.  
  64. } catch (ClassNotFoundException e) {
  65. System.out.println("Driver problem");
  66. } catch (SQLException e) {
  67. System.out.println(e);
  68. }
  69. }
  70.  
  71.  
  72. /**
  73. * Displays the data base
  74. */
  75. public void displayDB()
  76. {
  77. try {
  78. Class.forName("com.mysql.jdbc.Driver");
  79. Connection conn = null;
  80. conn = DriverManager.getConnection(url, user, password);
  81.  
  82. Statement st = conn.createStatement();
  83. String query = "SELECT * FROM generator_postaci_rpg1.klasa";
  84. ResultSet rs = st.executeQuery(query);
  85. ResultSetMetaData rsmd = rs.getMetaData();
  86. System.out.println(rsmd);
  87.  
  88. } catch (ClassNotFoundException e) {
  89. System.out.println("Driver problem");
  90. } catch (SQLException e) {
  91. System.out.println(e);
  92. }
  93. }
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. //-------------------------------------------------------------------------------------------------------------
  101. // usuwanie, edycja
  102. //-------------------------------------------------------------------------------------------------------------
  103.  
  104.  
  105.  
  106. /**
  107. * Removes film from data base
  108. *
  109. * @param f Object of Film class
  110. *//*
  111. public void deleteFilm(Film f)
  112. {
  113. try {
  114. Class.forName("com.mysql.jdbc.Driver");
  115. Connection conn = null;
  116. java.sql.PreparedStatement pst = null;
  117. conn = DriverManager.getConnection(url, user, password);
  118. if (conn != null) System.out.println("Connected");
  119. String sql = "DELETE FROM sakila.films WHERE Title ='" + f.title + "';";
  120. pst = conn.prepareStatement(sql);
  121.  
  122. pst.executeUpdate();
  123.  
  124. } catch (ClassNotFoundException e) {
  125. System.out.println("Driver problem");
  126. } catch (SQLException e) {
  127. System.out.println(e);
  128. }
  129. }
  130.  
  131. *//**
  132. * Changes title of chosen film
  133. *
  134. * @param f Object of Film class
  135. * @param title New title
  136. *//*
  137. public void editFilm(Film f, String title)
  138. {
  139. try {
  140. Class.forName("com.mysql.jdbc.Driver");
  141. Connection conn = null;
  142. java.sql.PreparedStatement pst = null;
  143. conn = DriverManager.getConnection(url, user, password);
  144. if (conn != null) System.out.println("Connected");
  145. String sql = "UPDATE sakila.films SET Title='" + title + "' WHERE Title='" + f.title + "';";
  146. pst = conn.prepareStatement(sql);
  147.  
  148. pst.executeUpdate();
  149.  
  150. f.title = title;
  151.  
  152. } catch (ClassNotFoundException e) {
  153. System.out.println("Driver problem");
  154. } catch (SQLException e) {
  155. System.out.println(e);
  156. }
  157. }
  158.  
  159. *//**
  160. * Adds rating to chosen films
  161. *
  162. * @param f Object of Film class
  163. * @param rate Rating to add
  164. *//*
  165. public void addRating(Film f, int rate)
  166. {
  167. try {
  168. Class.forName("com.mysql.jdbc.Driver");
  169. Connection conn = null;
  170. Statement st1 = null;
  171. Statement st2 = null;
  172. conn = DriverManager.getConnection(url, user, password);
  173. if (conn != null) System.out.println("Connected");
  174. String sql = "SELECT Rating FROM sakila.films WHERE Title='" + f.title + "';";
  175. st1 = conn.createStatement();
  176. st2 = conn.createStatement();
  177. String sql2 = "SELECT RatingNumber FROM sakila.films WHERE Title='" + f.title + "';";
  178. ResultSet rst = st1.executeQuery(sql);
  179. ResultSet rs = st2.executeQuery(sql2);
  180.  
  181. float rating;
  182. int number;
  183.  
  184. if (rst.next()) {
  185. rating = rst.getFloat("Rating");
  186.  
  187. if (rs.next()) {
  188. number = rs.getInt("RatingNumber");
  189.  
  190. float suma = rating * number;
  191. System.out.println(suma);
  192. float niu = (suma + rate) / number + 1;
  193. System.out.println(niu);
  194.  
  195. String sql3 = "UPDATE sakila.films SET Rating=" + niu + " WHERE Title='" + f.title + "';";
  196. String sql4 = "UPDATE sakila.films SET RatingNumber=" + (number + 1) + " WHERE Title='" + f.title + "';";
  197.  
  198. java.sql.PreparedStatement pst = null;
  199. pst = conn.prepareStatement(sql3);
  200. pst.executeUpdate();
  201. pst = conn.prepareStatement(sql4);
  202. pst.executeUpdate();
  203.  
  204. f.rating = niu;
  205. f.ratingNumber++;
  206. }
  207.  
  208.  
  209. }
  210.  
  211. } catch (ClassNotFoundException e) {
  212. System.out.println("Driver problem");
  213. } catch (SQLException e) {
  214. System.out.println(e);
  215. }
  216. }*/
  217. }
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242. public class Main {
  243.  
  244. public static void main(String[] args)
  245. {
  246. DataBase db = new DataBase();
  247. db.dodajKlase();
  248. db.displayDB();
  249. }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement