Advertisement
turbojez

baza

Jan 22nd, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. package com.company;
  2. //package jdbc;
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.ResultSetMetaData;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. //TODO: insert function
  12. public class JDBC {
  13. String driver = "com.mysql.cj.jdbc.Driver";
  14. String url = "jdbc:mysql://hostname:3306/kuku";
  15.  
  16. /**Loading driver*/
  17. static boolean loadDriver() {
  18. System.out.print("Checking driver...");
  19. try {
  20. Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
  21. return true;
  22. } catch (Exception e) {
  23. System.out.println("Error during loading a driver");
  24. return false;
  25. }
  26. }
  27.  
  28. /**
  29. * Method for connecting to database
  30. *
  31. * @param address
  32. * - address of the database
  33. * @param dataBaseName
  34. * - ..as above
  35. * @param userName
  36. * - login to database
  37. * @param password
  38. * - password to database
  39. * @return - connects with database
  40. */
  41. private static Connection connectToDatabase(String address,
  42. String dataBaseName, String userName, String password) {
  43. System.out.print("\nConnecting with database..:");
  44. String database = "jdbc:mysql://" + address + "/" + dataBaseName;
  45. java.sql.Connection connection = null;
  46. try {
  47. connection = DriverManager.getConnection(database, userName, password);
  48. } catch (SQLException e) {
  49. System.out.println("Error during connecting to database");
  50. System.exit(1);
  51. }
  52. return connection;
  53. }
  54. /**
  55. * Displays data from SELECT query
  56. *
  57. * @param r
  58. * - result
  59. */
  60. private static void printDataFromQuery(ResultSet r) {
  61. ResultSetMetaData rsmd;
  62. try {
  63. rsmd = r.getMetaData();
  64.  
  65. int numcols = rsmd.getColumnCount();
  66.  
  67. // display names of columns:
  68. for (int i = 1; i <= numcols; i++) {
  69. System.out.print("\t" + rsmd.getColumnLabel(i) + "\t|");
  70. }
  71. System.out.print("\n____________________________________________________________________________\n");
  72. //r.next() - jump to next record
  73. while (r.next()) {
  74. for (int i = 1; i <= numcols; i++) {
  75. Object obj = r.getObject(i);
  76. if (obj != null)
  77. System.out.print("\t" + obj.toString() + "\t|");
  78. else
  79. System.out.print("\t");
  80. }
  81. System.out.println();
  82. }
  83. } catch (SQLException e) {
  84. System.out.println("Error during reading from database" + e.toString());
  85. System.exit(3);
  86. }
  87. }
  88.  
  89. /**
  90. * Wykonanie kwerendy i przesłanie wyników do obiektu ResultSet
  91. *
  92. * @param s
  93. * - Statement
  94. * @param sql
  95. * - zapytanie
  96. * @return wynik
  97. */
  98. private static ResultSet executeQuery(Statement s, String sql) {
  99. try {
  100. return s.executeQuery(sql);
  101. } catch (SQLException e) {
  102. e.printStackTrace();
  103. }
  104. return null;
  105. }
  106.  
  107. /**
  108. * tworzenie obiektu Statement przesyłającego zapytania do bazy connection
  109. *
  110. * @param connection
  111. * - połączenie z bazą
  112. * @return obiekt Statement przesyłający zapytania do bazy
  113. */
  114. private static Statement createStatement(Connection connection) {
  115. try {
  116. return connection.createStatement();
  117. } catch (SQLException e) {
  118. e.printStackTrace();
  119. }
  120. ;
  121. return null;
  122. }
  123.  
  124. /**
  125. * Zamykanie połączenia z bazą danych
  126. *
  127. * @param connection
  128. * - połączenie z bazą
  129. * @param s
  130. * - obiekt przesyłający zapytanie do bazy
  131. */
  132. private static void closeConnection(Connection connection, Statement s) {
  133. System.out.print("\nZamykanie polaczenia z databaseą:");
  134. try {
  135. s.close();
  136. connection.close();
  137. } catch (SQLException e) {
  138. System.out
  139. .println("Bląd przy zamykaniu polączenia " + e.toString());
  140. System.exit(4);
  141. }
  142. System.out.print(" zamknięcie OK");
  143. }
  144.  
  145. public JDBC(){
  146. if (loadDriver())
  147. System.out.print("Driver correct");
  148. else
  149. System.exit(1);
  150. java.sql.Connection connection = connectToDatabase("127.0.0.1",
  151. "kuku", "root", "");
  152. if (connection != null)
  153. System.out.print("Connection correct\n");
  154.  
  155. // WYKONYWANIE OPERACJI NA BAZIE DANYCH
  156. System.out.println("Pobieranie danych z bazy:");
  157. //String sql = "INSERT INTO Winnings (name, won) VALUES ('pawel', 2)";
  158. String sql = "Select * from Winnings";
  159. Statement s = createStatement(connection);
  160. ResultSet r = executeQuery(s, sql);
  161. printDataFromQuery(r);
  162. closeConnection(connection, s);
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement