Using Databasemetadate to get database information

Source: Internet
Author: User
Tags mathematical functions table name
Data | database

Import java.sql.*;
public class Getdbinfo
{
Public Getdbinfo ()
{
}

public static void Main (string[] args)
{
try{
Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver"). newinstance ();
String url= "Jdbc:microsoft:sqlserver://gaoxiang:1433;databasename=master";
String user= "daryl715";
String pass= "1234";
String s;
Connection con=drivermanager.getconnection (Url,user,pass);
DatabaseMetaData Dbmd=con.getmetadata ();
s = Dbmd.getdrivername ();
System.out.println ("Driver name is:" +s);
System.out.println ("");

s = Dbmd.getdatabaseproductname ();
SYSTEM.OUT.PRINTLN ("Database name is:" +s);
System.out.println ("");

ResultSet rs = Dbmd.getschemas ();
System.out.println ("Pattern name has:");
while (Rs.next ())
System.out.print ("" +rs.getstring (1));
System.out.println ();

s = dbmd.getsqlkeywords ();
SYSTEM.OUT.PRINTLN ("The keyword in SQL is:" +s);
System.out.println ("");

int Max=dbmd.getmaxcolumnnamelength ();
System.out.println ("The maximum length of the column name can be:" +max);
System.out.println ("");

max = Dbmd.getmaxtablenamelength ();
System.out.println ("The maximum length of the table name can be:" +max);
System.out.println ("");

max = Dbmd.getmaxcolumnsinselect ();
System.out.println ("The maximum length of the number of column names that can be returned by a SELECT clause is:" +max);
System.out.println ("");

max = Dbmd.getmaxtablesinselect ();
System.out.println ("The maximum number of tables a SELECT statement can access:" +max);
System.out.println ("");


max = Dbmd.getmaxcolumnsintable ();
SYSTEM.OUT.PRINTLN ("Maximum number of columns allowed in the table:" +max);
System.out.println ("");

max = Dbmd.getmaxconnections ();
System.out.println ("Number of concurrent accessed users:" +max);
System.out.println ("");

max = Dbmd.getmaxstatementlength ();
System.out.println ("Maximum allowable length of SQL statement:" +max);
System.out.println ("");

s = dbmd.getnumericfunctions ();
SYSTEM.OUT.PRINTLN ("List of all mathematical functions of the database:" +s);
System.out.println ("");

s = dbmd.getstringfunctions ();
SYSTEM.OUT.PRINTLN ("List of all string functions of the database:" +s);
System.out.println ("");

s = dbmd.getsystemfunctions ();
SYSTEM.OUT.PRINTLN ("List of all system functions of the database:" +s);
System.out.println ("");

s = dbmd.gettimedatefunctions ();
SYSTEM.OUT.PRINTLN ("List of all date-time functions of the database:" +s);
System.out.println ("");

rs = Dbmd.gettypeinfo ();
while (Rs.next ())
{
System.out.print ("Data type name:" +rs.getstring (1));
System.out.print ("Data type:" + rs.getstring (2));
System.out.print ("precision:" + rs.getstring (3));
System.out.println ("Base:" + rs.getstring (18));
}
System.out.println ("");

s = Dbmd.geturl ();
System.out.println ("URL of this database:" +s);
System.out.println ("");

s = Dbmd.getusername ();
System.out.println ("User of this database:" +s);
System.out.println ("");

    String [] t = {"TABLE", "VIEW"};
    rs = dbmd.gettables (null, "HR", "%", t);
    while (Rs.next ()) {
      System.out.print ("Directory Name:" +rs.getstring ( 1));
      System.out.print ("Schema name:" +rs.getstring (2));
      System.out.print ("Table name:" +rs.getstring (3));
      System.out.print ("Type of Table:" +rs.getstring (4));
      System.out.println ("Note:" +rs.getstring (5));
     }
      System.out.println ("");

    rs = Dbmd.getprimarykeys (null, "HR", "EMPLOYEES");
    while (Rs.next ()) {
      System.out.print ("Directory Name:" +rs.getstring ( 1));
      System.out.print ("Schema name:" +rs.getstring (2));
      System.out.print ("Table name:" +rs.getstring (3));
      System.out.print ("Column name Order number:" +rs.getstring (4));
      System.out.print ("Column name Order number:" +rs.getstring (5));
      System.out.println ("PRIMARY Key Name:" +rs.getstring (6));
     }
      System.out.println ("");

rs = Dbmd.gettabletypes ();
System.out.println ("Type of table has:");
while (Rs.next ())
System.out.print ("" + rs.getstring (1));
System.out.println ();
System.out.println ("");

rs = Dbmd.getcolumns (null, "HR", "EMPLOYEES", "%");
System.out.println (table name + column name + data type + "local type name" + "column size" + "decimal place" + "Data base" + "is nullable" + "index number");
while (Rs.next ()) {
System.out.print (rs.getstring (3) + "");
System.out.print (Rs.getstring (4) + "");
System.out.print (rs.getstring (5) + "");
System.out.print (rs.getstring (6) + "");
System.out.print (rs.getstring (7) + "");
System.out.print (rs.getstring (9) + "");
System.out.print (rs.getstring (10) + "");
System.out.print (rs.getstring (11) + "");
System.out.println (Rs.getstring (17) + "");
}
System.out.println ("");

rs = Dbmd.getindexinfo (null, "HR", "EMPLOYEES", false, false);

SYSTEM.OUT.PRINTLN ("Table name" + "index" + "index type" + "indexed column name" + "index order" + "decimal digits" + "data base" + "is nullable" + "index number");
while (Rs.next ()) {
System.out.print (rs.getstring (3) + "");
System.out.print (rs.getstring (6) + "");
System.out.print (rs.getstring (7) + "");
System.out.print (rs.getstring (9) + "");
System.out.println (rs.getstring (10) + "");
}
System.out.println ("");

Rs.close ();
Con.close ();
}
catch (Exception e) {System.out.println (e);}
New Getdbinfo ();
}
}




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.