Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package birdShop.DBManagers;
- import java.sql.*;
- /**
- * Created by Byblik272 on 28/4/2016.
- */
- public class DerbyManager {
- public static void main(String[] args) {
- DerbyManager derbyManager = new DerbyManager();
- derbyManager.connectToSoundDB();
- }
- private final String basePath = "jdbc:derby:BirdShopDB;create=true;shutdown=true";
- private final String dropTable = "DROP TABLE sound";
- private final String soundTable = "CREATE TABLE sound(id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), " +
- "sound BOOLEAN)";
- private final String insertValue = "INSERT INTO sound (sound) VALUES(true)";
- private final String deleteValue = "DELETE FROM sound WHERE id=2";
- private final String GET_ALL = "SELECT * FROM sound";
- private Connection connection;
- private Statement statement;
- private PreparedStatement preparedStatement;
- private ResultSet resultSet;
- public DerbyManager() {
- try {
- connection = DriverManager.getConnection(basePath);
- statement = connection.createStatement();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void setVolume(final boolean volume) {
- try {
- statement.execute("UPDATE sound SET sound = " + volume + " WHERE id = 1");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void connectToSoundDB() {
- try {
- // connection = DriverManager.getConnection("jdbc:derby:BirdShopDB;shutdown=true");
- // connection = DriverManager.getConnection(basePath);
- // statement = connection.createStatement();
- // statement.executeUpdate(soundTable);
- statement.execute(dropTable);
- // statement.executeUpdate(deleteValue);
- preparedStatement = connection.prepareStatement(GET_ALL);
- resultSet = preparedStatement.executeQuery();
- while (resultSet.next()) {
- System.out.println(resultSet.getInt("id") + " " + resultSet.getBoolean("sound"));
- }
- connection.close();
- statement.close();
- preparedStatement.close();
- resultSet.close();
- // connection = DriverManager.getConnection("jdbc:derby:BirdShopDB;shutdown=true");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement