Вы находитесь на странице: 1из 3

/* * To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates * and open the template in the editor. */ package example.mediascanner.dao; import import import import import import java.sql.PreparedStatement; java.sql.SQLException; java.util.logging.Level; org.hariseldon.incubator.mediascanner.Scanner; org.slf4j.Logger; org.slf4j.LoggerFactory;

/** * */ public class DerbyCRUD { final static private Logger logger = LoggerFactory.getLogger(DerbyCRUD.class); static private DerbyCRUD instance; private java.sql.Connection connection; static public DerbyCRUD getInstance() { if (instance == null) { instance = new DerbyCRUD(); } return instance; } private DerbyCRUD() { } public java.sql.Connection getConnection() { if (connection == null) { try { connection = java.sql.DriverManager.getConnection("jdbc:derby:media_scan ;create=true"); } catch (SQLException ex) { logger.error("Error getting connection", ex); } } return connection; } public void create() { String sql; sql = "CREATE TABLE IF NOT EXISTS tbl_Partitions (" + "id INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START W ITH 1, INCREMENT BY 1), " + "uid VARCHAR(255) NOT NULL, " + "rawPath VARCHAR(255) NOT NULL, " + "dir VARCHAR(100) NOT NULL, " + "name VARCHAR(40) NOT NULL, " + "type VARCHAR(8) NOT NULL, " + "size BIGINT NOT NULL, " + ");";

try (PreparedStatement preparedStatement = getConnection().prepareStatement( sql)) { preparedStatement.executeQuery(); } catch (SQLException ex) { logger.error("Create tbl_Partitions error", ex); } sql = "CREATE TABLE IF NOT EXISTS tbl_Files (" + "id INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START W ITH 1, INCREMENT BY 1), " + "dir VARCHAR(255) NOT NULL, " + "name VARCHAR(255) NOT NULL, " + "size BIGINT NOT NULL, " + "checksum BIGINT" + ");"; try (PreparedStatement preparedStatement = getConnection().prepareStatement( sql)) { preparedStatement.executeQuery(); } catch (SQLException ex) { logger.error("Create tbl_Files error", ex); } } //create public void drop() { String sql; sql = "DROP TABLE tbl_Files"; try (PreparedStatement preparedStatement = getConnection().prepareStatement( sql)) { preparedStatement.executeQuery(); } catch (SQLException ex) { logger.error("Drop error", ex); } sql = "DROP TABLE tbl_Partitions"; try (PreparedStatement preparedStatement = getConnection().prepareStatement( sql)) { preparedStatement.executeQuery(); } catch (SQLException ex) { logger.error("Drop error", ex); } } //drop public void clear() { String sql1 = "DELETE FROM tbl_Files"; String sql2 = "DELETE FROM tbl_Partitions"; try ( PreparedStatement preparedStatement1 = getConnection().prepareStatem ent(sql1); PreparedStatement preparedStatement2 = getConnection().prepareStatem ent(sql2)) { preparedStatement1.executeQuery(); preparedStatement2.executeQuery(); } catch (SQLException ex) { java.util.logging.Logger.getLogger(Scanner.class.getName()).log(Level.SEVE

RE, null, ex); } } }

Вам также может понравиться