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

How to determine whether a table exists in java?

Release: 2019-11-20 16:27:40
Original
4222 people have browsed it

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;
           }
     }
Copy after login

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;
         }
     }
Copy after login

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';
Copy after login

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';
Copy after login

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!

Related labels:
Statement of this Website
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
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template