0

我正在开发一个程序JavaDB(它包含在 1.6+ 版本的 jdk 中)。

在我的程序中,在它运行之前,它会创建数据库和表。问题是当我运行这个程序然后关闭它并再次运行时,会发生错误(因为已经存在同名的表)。

我的代码是这样的(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方法正是我想要做的。我在 Internet 上看到了他们在 MySQL 中运行良好的查询。所以我相信德比也有类似的命令。

4

1 回答 1

0

我解决了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

于 2013-10-17T03:08:16.663 回答