簡體   English   中英

如何在derby(JavaDB)中獲取information_schema?

[英]How to get information_schema in derby(JavaDB)?

我正在使用JavaDB (在1.6+版的jdk中包含)進行程序開發。

在我的程序中,它在運行之前會創建數據庫和表。 問題是當我運行該程序然后將其關閉並再次運行時,會發生錯誤(因為已經存在具有相同名稱的表)。

我的代碼是這樣的(帶有SpringFramework的Java):

public class DBManager {
        private Connection conn = null;
        private PreparedStatement pstmt = null;
        private ResultSet rs = null;

    private Logger log = Logger.getLogger(DBManager.class);
    // JavaDB Embed Driver
    private String driver = "org.apache.derby.jdbc.EmbeddedDriver";    
    // Database name
    private String dbName = "IIS_M_DB";
    // Table name
    private String tableName = "tbl_iis";    
    // ConnectionURL
    private String connectionURL = "jdbc:derby:" + dbName + ";create=true";

    // Create Table Query
    private String createString = "CREATE TABLE " + tableName 
            + "(con_id VARCHAR(40) constraint iismgr_pk primary key,"
            + "file_name VARCHAR(200),"
            + "result VARCHAR(30) default 'YET',"
            + "udt_date TIMESTAMP default CURRENT_TIMESTAMP,"
            + "reg_date TIMESTAMP default CURRENT_TIMESTAMP)";

    public void getConnection(){
        try{
            log.info("get Connection with : " + connectionURL);
            conn = DriverManager.getConnection(connectionURL);
        }catch(Exception e){
            log.error("failed to connect : " + connectionURL);
            e.printStackTrace();
        }
    } 

    public boolean checkTable(){
        boolean result = false;
        String check = "select count(*) from information_schema.tables "
                + "where table_schema = '" 
                + dbName + "' and table_name = '" 
                + tableName + "'";
        try{
            pstmt = conn.prepareStatement(check);
            rs = pstmt.executeQuery();
            int resultInt = rs.getInt(0);
            if(resultInt < 1){
                result = true;
            }
        }catch(Exception e){e.printStackTrace();}
        return result;
    }

    public void createDB(){
        try{
            log.info("Get Driver.");
            Class.forName(driver);
            getConnection();
        }catch(Exception e){
            log.error("failed to get Driver.");
            e.printStackTrace();
        }
    }

    public void createTable(){
        try{
            log.info("creating Table.");
            pstmt = conn.prepareStatement(createString);
            pstmt.execute();
            pstmt.close();
        }catch(Exception e){
            log.error("failed to create table : " + tableName);
            e.printStackTrace();
        }
    }

    public void disconnect(){
        try{
            if(conn != null)
                conn.close();
        }catch(Exception e){
            log.error("failed to disconnect DB.");
            e.printStackTrace();}
    }

    //만약을 위한 셧다운 함수.
    public void shutDown(){
        if(driver.equals("org.apache.derby.jdbc.EmbeddedDriver")){
            boolean isSuccess = false;
            try{
                log.info("ShutDown DB.");
                DriverManager.getConnection("jdbc:derby:;shutdown=true");
            }catch(SQLException e){
                if(e.getSQLState().equals("XJ015"))
                    isSuccess = true;
                log.error("failed to shutDown DB.");
                e.printStackTrace();
            }
            if(!isSuccess)
                log.error("failed to shutDown DB.");
        }
    }
} 

checkTable方法正是我想要做的。 而且我在Internet上看到了該查詢,它們在MySQL中運行良好。 因此,我相信Derby中也有類似的命令。

我解決了XD。 但是以不同的方式,不使用查詢。

我編輯了例外區域,如下所示。

public void createTable(){
        try{
            log.info("creating Table.");
            pstmt = conn.prepareStatement(createString);
            pstmt.execute();
            pstmt.close();
        }catch(SQLException e){
            if(e.getSQLState().equals("X0Y32")){
                log.info("table " + tableName + " is already exist.");
            }
        }
    }

如果其他查詢方法,我會選擇它作為這個問題的答案:D

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM