Home  >  Article  >  Java  >  How to determine whether a table exists in java?

How to determine whether a table exists in java?

尚
Original
2019-11-20 16:27:404006browse

How to determine whether a table exists in java?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn