Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. package birdShop.DBManagers;
  2.  
  3. import java.sql.*;
  4.  
  5. /**
  6. * Created by Byblik272 on 28/4/2016.
  7. */
  8. public class DerbyManager {
  9.  
  10. public static void main(String[] args) {
  11. DerbyManager derbyManager = new DerbyManager();
  12. derbyManager.connectToSoundDB();
  13.  
  14. }
  15.  
  16. private final String basePath = "jdbc:derby:BirdShopDB;create=true;shutdown=true";
  17. private final String dropTable = "DROP TABLE sound";
  18. private final String soundTable = "CREATE TABLE sound(id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), " +
  19. "sound BOOLEAN)";
  20. private final String insertValue = "INSERT INTO sound (sound) VALUES(true)";
  21. private final String deleteValue = "DELETE FROM sound WHERE id=2";
  22. private final String GET_ALL = "SELECT * FROM sound";
  23.  
  24. private Connection connection;
  25. private Statement statement;
  26. private PreparedStatement preparedStatement;
  27. private ResultSet resultSet;
  28.  
  29. public DerbyManager() {
  30. try {
  31. connection = DriverManager.getConnection(basePath);
  32. statement = connection.createStatement();
  33. } catch (SQLException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37.  
  38. public void setVolume(final boolean volume) {
  39. try {
  40. statement.execute("UPDATE sound SET sound = " + volume + " WHERE id = 1");
  41. } catch (SQLException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. public void connectToSoundDB() {
  46. try {
  47. // connection = DriverManager.getConnection("jdbc:derby:BirdShopDB;shutdown=true");
  48. // connection = DriverManager.getConnection(basePath);
  49.  
  50. // statement = connection.createStatement();
  51. // statement.executeUpdate(soundTable);
  52. statement.execute(dropTable);
  53. // statement.executeUpdate(deleteValue);
  54. preparedStatement = connection.prepareStatement(GET_ALL);
  55. resultSet = preparedStatement.executeQuery();
  56.  
  57. while (resultSet.next()) {
  58. System.out.println(resultSet.getInt("id") + " " + resultSet.getBoolean("sound"));
  59. }
  60.  
  61. connection.close();
  62. statement.close();
  63. preparedStatement.close();
  64. resultSet.close();
  65. // connection = DriverManager.getConnection("jdbc:derby:BirdShopDB;shutdown=true");
  66. } catch (SQLException e) {
  67. e.printStackTrace();
  68. }
  69. }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement