Java obtains the name of the table in the database, and java obtains the database.
I. Java method
// Obtain the names of all tables in the current database.
Java code
- Public void getTableNameByCon (Connection con ){
- Try {
- DatabaseMetaData meta = con. getMetaData ();
- ResultSet rs = meta. getTables (null,
- New String [] {"TABLE "});
- While (rs. next ()){
- System. out. println ("table name:" + rs. getString (3 ));
- System. out. println ("Table Username:" + rs. getString (2 ));
- System. out. println ("------------------------------");
- }
- Con. close ();
- } Catch (Exception e ){
- Try {
- Con. close ();
- } Catch (SQLException e1 ){
- // TODO Auto-generated catch block
- E1.printStackTrace ();
- }
- // TODO Auto-generated catch block
- E. printStackTrace ();
- }
- }
2. Obtain the names of all tables in the database using SQL statements.
-- MySQL database
Java code
- Show tables;
-- Oracle Database (table under the current Login User)
Java code
- Select uo. object_name from user_objects uo where uo. object_type = 'table ';
-- Database of SQL server 2000/2005
Java code
- Select name from sysobjects
-- Sybase Database
Java code
- Select name from sysobjects where type = 'U ';
-- DB2 database
Java code
- Select tabname from syscat. tables where tabschema = current schema ;//
Obtains all user tables in the current mode.