在MySQL中创建并插入临时表

时间:2014-04-15 16:12:27

标签: mysql

我想在我的sql Version 5.5.27中创建和插入临时表,临时表是一个表,当myprogram中的函数或按钮被调用时,该表会出现。那么,如何制作那个临时表呢?

1 个答案:

答案 0 :(得分:0)

这是指南 - http://www.xyzws.com/Javafaq/how-to-use-jdbc-java-to-create-table/170

基本上你想用java执行你的create sql,这是上面链接代码的主要部分:

    Connection con = getConnection();
    Statement stmt =null;
    String createString;
    createString = "CREATE TABLE  `mydb`.`employees` ("+
    "`EmployeeID` int(10) unsigned NOT NULL default '0',"+
    "`Name` varchar(45) collate utf8_unicode_ci NOT NULL default '',"+
    "`Office` varchar(10) collate utf8_unicode_ci NOT NULL default '',"+
    "`CreateTime` timestamp NOT NULL default CURRENT_TIMESTAMP,"+
    "PRIMARY KEY  (`EmployeeID`)"+
     ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";            
    try {
        stmt = con.createStatement();
        stmt.executeUpdate(createString);
    } catch(SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
    finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                System.err.println("SQLException: " + e.getMessage());
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                System.err.println("SQLException: " + e.getMessage());
            }
        }
    }