Advertisement
Guest User

Untitled

a guest
Dec 5th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.Properties;
  7.  
  8. public class JDBC {
  9.  
  10. static boolean ladujSterownik() {
  11. System.out.print("Sprawdzanie sterownika:");
  12. try {
  13. Class.forName("com.mysql.jdbc.Driver").newInstance();
  14. return true;
  15. } catch (Exception e) {
  16. System.out.println("Blad przy ladowaniu sterownika bazy!");
  17. return false;
  18. }
  19. }
  20.  
  21. public static Connection getConnection(String kindOfDatabase, String adres, int port, String userName,
  22. String password) {
  23.  
  24. Connection conn = null;
  25. Properties connectionProps = new Properties();
  26. connectionProps.put("user", userName);
  27. connectionProps.put("password", password);
  28. try {
  29. conn = DriverManager.getConnection(kindOfDatabase + adres + ":" + port + "/", connectionProps);
  30. } catch (SQLException e) {
  31. System.out.println("Błąd połączenia z bazą danych! " + e.getMessage() + ": " + e.getErrorCode());
  32. System.exit(2);
  33. }
  34. System.out.println("Połączenie z bazą danych: ... OK");
  35. return conn;
  36. }
  37.  
  38. private static Statement createStatement(Connection connection) {
  39. try {
  40. return connection.createStatement();
  41. } catch (SQLException e) {
  42. System.out.println("Błąd createStatement! " + e.getMessage() + ": " + e.getErrorCode());
  43. System.exit(3);
  44. }
  45. return null;
  46. }
  47.  
  48. private static int executeUpdate(Statement s, String sql) {
  49. try {
  50. return s.executeUpdate(sql);
  51. } catch (SQLException e) {
  52. System.out.println("Zapytanie nie wykonane! " + e.getMessage() + ": " + e.getErrorCode());
  53. }
  54. return -1;
  55. }
  56.  
  57. private static ResultSet executeQuery(Statement s, String sql) {
  58. try {
  59. return s.executeQuery(sql);
  60. } catch (SQLException e) {
  61. System.out.println("Zapytanie nie wykonane! " + e.getMessage() + ": " + e.getErrorCode());
  62. }
  63. return null;
  64. }
  65.  
  66. private static void closeConnection(Connection connection, Statement s) {
  67. System.out.print("\nZamykanie polaczenia z bazą:");
  68. try {
  69. s.close();
  70. connection.close();
  71. } catch (SQLException e) {
  72. System.out.println("Bląd przy zamykaniu polączenia z bazą! " + e.getMessage() + ": " + e.getErrorCode());
  73. ;
  74. System.exit(4);
  75. }
  76. System.out.print(" zamknięcie OK");
  77. }
  78. public static void dodajAnkiete(Statement st)
  79. {
  80. String sql;
  81. ResultSet rs;
  82. if (executeUpdate(st,
  83. "CREATE TABLE ankieta ( id INT NOT NULL, nazwaAnkiety VARCHAR(50) NOT NULL, PRIMARY KEY (id) );") == 0)
  84. System.out.println("Tabela utworzona");
  85.  
  86. else
  87. System.out.println("Tabela nie utworzona!");
  88.  
  89. sql = "SELECT * FROM ANKIETA";
  90. rs = executeQuery(st,sql);
  91.  
  92. try {
  93. if(!rs.next())
  94. {
  95. sql = "INSERT INTO ankieta VALUES(1,'Ankieta na temat komunikacji miejskiej');";
  96. executeUpdate(st, sql);
  97. }
  98. } catch (SQLException e) {
  99. e.printStackTrace();
  100. }
  101.  
  102. if (executeUpdate(st,
  103. "CREATE TABLE pytanie ( id INT NOT NULL, idAnkiety INT, pytanie VARCHAR(80) NOT NULL,"
  104. + "odpa VARCHAR(30) NOT NULL ,odpb VARCHAR(30) NOT NULL,odpc VARCHAR(30) NOT NULL"
  105. + ",odpd VARCHAR(30) NOT NULL, PRIMARY KEY (id)," +
  106. " FOREIGN KEY (idAnkiety) REFERENCES ankieta(id) );") == 0)
  107. System.out.println("Tabela utworzona");
  108. else
  109. System.out.println("Tabela nie utworzona!");
  110.  
  111. sql = "SELECT * FROM PYTANIE";
  112. rs = executeQuery(st,sql);
  113.  
  114. try {
  115. if(!rs.next())
  116. {
  117. sql = "INSERT INTO pytanie VALUES(1,1,'Jak czesto korzystasz z komunikacji miejskiej','codziennie','dwa razy w tygodniu','raz w miesiacu','nigdy');";
  118. executeUpdate(st, sql);
  119. sql = "INSERT INTO pytanie VALUES(2,1,'Jak w skali 1-4 oceniasz komunikacje (1-zle , 4 -bardzo dobrze)','1','2','3','4');";
  120. executeUpdate(st, sql);
  121. }
  122. } catch (SQLException e) {
  123. e.printStackTrace();
  124. }
  125. }
  126. public static void print(Statement st)
  127. {
  128. String sql;
  129. ResultSet rs;
  130. sql = "SELECT * FROM ANKIETA";
  131. rs = executeQuery(st,sql);
  132. Ankieta a;
  133. Pytanie p;
  134. try {
  135. while(rs.next())
  136. {
  137. int id = rs.getInt(1);
  138. String nazwa = rs.getString(2);
  139. a = new Ankieta(id,nazwa);
  140. System.out.println(a);
  141. }
  142. } catch (SQLException e) {
  143. e.printStackTrace();
  144. }
  145.  
  146.  
  147. sql = "SELECT * FROM PYTANIE";
  148. rs = executeQuery(st,sql);
  149.  
  150. try {
  151. while(rs.next())
  152. {
  153. int id = rs.getInt(1);
  154. String pytanie = rs.getString(2);
  155. String pytanie = rs.getString(2);
  156.  
  157. }
  158. } catch (SQLException e) {
  159. e.printStackTrace();
  160. }
  161.  
  162.  
  163. }
  164.  
  165. public static void main(String[] args) {
  166. if (ladujSterownik()) {
  167. System.out.println("Polaczono z baza");
  168. } else {
  169. System.exit(1);
  170. }
  171.  
  172. Connection con = getConnection("jdbc:mysql://", "localhost", 3306, "root", "");
  173. Statement st = createStatement(con);
  174. ResultSet rs;
  175. String sql;
  176.  
  177. if (executeUpdate(st, "USE nowabaza1;") == 0)
  178. System.out.println("Baza wybrana");
  179. else {
  180. System.out.println("Baza nie istnieje! Tworzymy bazę: ");
  181. if (executeUpdate(st, "create Database nowabaza1;") == 1)
  182. System.out.println("Baza utworzona");
  183. else
  184. System.out.println("Baza nieutworzona!");
  185. if (executeUpdate(st, "USE nowaBaza;") == 0)
  186. System.out.println("Baza wybrana");
  187. else
  188. System.out.println("Baza niewybrana!");
  189. }
  190. dodajAnkiete(st);
  191. print(st);
  192.  
  193.  
  194.  
  195.  
  196.  
  197. }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement