简体   繁体   English

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

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

I am working on a program using JavaDB (which is include within the jdk in version 1.6+). 我正在使用JavaDB (在1.6+版的jdk中包含)进行程序开发。

In my program, before it runs, it creates the DB and Table. 在我的程序中,它在运行之前会创建数据库和表。 The problem is when I run this program and then shut it down and run again, an error occurs (because a table with same name already exists). 问题是当我运行该程序然后将其关闭并再次运行时,会发生错误(因为已经存在具有相同名称的表)。

My code is like such (Java with SpringFramework) : 我的代码是这样的(带有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 method is exactly what I am trying to do. checkTable方法正是我想要做的。 And I saw that query on the Internet which they is working nicely in MySQL. 而且我在Internet上看到了该查询,它们在MySQL中运行良好。 So I believe there is a similar command in Derby as well. 因此,我相信Derby中也有类似的命令。

I solved XD. 我解决了XD。 But in different way, not using query. 但是以不同的方式,不使用查询。

I edited Exception area like below. 我编辑了例外区域,如下所示。

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.");
            }
        }
    }

If other method with query, I will choice that as an answer of this question :D 如果其他查询方法,我会选择它作为这个问题的答案:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM