Determine whether a certain table in the database exists in Java code:
1. Use JdbcTemplate bean
public boolean validateTableNameExist(String tableName) { int tableNum = jdbcTemplate.queryForInt("SELECT COUNT(*) FROM ALL_TABLES WHERE TABLE_NAME=" + tableName); if (tableNum > 0) { return true; }else { return false; } }
2. Use Connection object
public boolean validateTableNameExist(String tableName) { Connection con = getYourCnnection; ResultSet rs = con.getMetaData().getTables(null, null, tableName, null); if (rs.next()) { return true; }else { return false; } }
Note:
1. Check whether a certain field exists in a certain table, pay attention to the capitalization
select count(*) from User_Tab_Columns where table_name='TABLENAME' and column_name='COLUMNNAME';
2. Check whether a certain table exists in a certain database, Note that the table name should be capitalized
select count(*) from all_tables where table_name='TABLENAME';
For more java knowledge, please pay attention to java basic tutorial.
The above is the detailed content of How to determine whether a table exists in java?. For more information, please follow other related articles on the PHP Chinese website!