Search code examples
javadatabasespringderby

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+).

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) :

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. And I saw that query on the Internet which they is working nicely in MySQL. So I believe there is a similar command in Derby as well.


Solution

  • I solved 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