Java obtains the name of the table in the database, and java obtains the database.

Source: Internet
Author: User
Tags sybase database

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
  1. Public void getTableNameByCon (Connection con ){
  2. Try {
  3. DatabaseMetaData meta = con. getMetaData ();
  4. ResultSet rs = meta. getTables (null,
  5. New String [] {"TABLE "});
  6. While (rs. next ()){
  7. System. out. println ("table name:" + rs. getString (3 ));
  8. System. out. println ("Table Username:" + rs. getString (2 ));
  9. System. out. println ("------------------------------");
  10. }
  11. Con. close ();
  12. } Catch (Exception e ){
  13. Try {
  14. Con. close ();
  15. } Catch (SQLException e1 ){
  16. // TODO Auto-generated catch block
  17. E1.printStackTrace ();
  18. }
  19. // TODO Auto-generated catch block
  20. E. printStackTrace ();
  21. }
  22. }

2. Obtain the names of all tables in the database using SQL statements.
-- MySQL database

Java code
  1. Show tables;

-- Oracle Database (table under the current Login User)

Java code
  1. Select uo. object_name from user_objects uo where uo. object_type = 'table ';

-- Database of SQL server 2000/2005

Java code
  1. Select name from sysobjects

-- Sybase Database

Java code
  1. Select name from sysobjects where type = 'U ';

-- DB2 database

Java code
  1. Select tabname from syscat. tables where tabschema = current schema ;//

Obtains all user tables in the current mode.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.