Created
July 24, 2014 01:08
-
-
Save yanhua365/089df960bf741b2bcbed to your computer and use it in GitHub Desktop.
判断数据库里有没有某个表,没有就创建
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private boolean tableExistsInDb(String tableName){ | |
Connection conn = null; | |
ResultSet tabs = null; | |
try { | |
conn = jdbcTemplate.getDataSource().getConnection(); | |
DatabaseMetaData dbMetaData = conn.getMetaData(); | |
String[] types = { "TABLE" }; | |
tabs = dbMetaData.getTables(null, null, tableName, types); | |
if (tabs.next()) { | |
return true; | |
} | |
} catch (Exception e) { | |
logger.error("Query DatabaseMetaData error.",e); | |
}finally{ | |
try { | |
tabs.close(); | |
conn.close(); | |
} catch (SQLException e) { | |
//do nothing. | |
} | |
} | |
return false; | |
} | |
private void createRecordTable(String tableName) { | |
StringBuffer sb = new StringBuffer(300); | |
sb.append("CREATE TABLE `" + tableName + "` ("); | |
sb.append("id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,\n"); | |
sb.append(" reader_id VARCHAR(50) NOT NULL,\n"); | |
sb.append("antenna_no INT NOT NULL,\n"); | |
sb.append("epc VARCHAR(50) NOT NULL,\n"); | |
sb.append("reaction_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,\n"); | |
sb.append("state INT NOT NULL\n"); | |
sb.append(")engine=MyISAM;"); | |
try { | |
jdbcTemplate.update(sb.toString()); | |
} catch (Exception e) { | |
logger.error("Create table["+tableName+"] error", e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment