Using the metadata Analysis database in the Java Basic series 15:JDBC

Source: Internet
Author: User

(1) DatabaseMetaData:

package javase.jdbc;import java.sql.connection;import java.sql.databasemetadata;import  Java.sql.resultset;import java.sql.sqlexception;public class databasemetadatademo {public  static void main (String[] args)  {Connection connection =  Jdbcconnection.getconnection (); Try {databasemetadata dmetadata = connection.getmetadata ();  //  Instantiate meta Data System.out.println ("Database name:"  + dmetadata.getdatabaseproductname ()); SYSTEM.OUT.PRINTLN ("database version: " + dmetadata.getdatabaseproductversion ()); Resultset resultset = dmetadata.getprimarykeys (null, null,  "users");while  ( Resultset.next ())  {system.out.println ("Table Category:"  + resultset.getstring (1)); SYSTEM.OUT.PRINTLN ("Table pattern:"  + resultset.getstring (2)); SYSTEM.OUT.PRINTLN ("Table name:"  + resultset.getstring (3)); SYSTEM.OUT.PRINTLN ("Column name:"  + resultset.getstring (4)); SYSTEM.OUT.PRINTLN ("PRIMARY key:" &Nbsp;+ resultset.getstring (5)); SYSTEM.OUT.PRINTLN ("PRIMARY Key Name:"  + resultset.getstring (6));}}  catch  (sqlexception e)  {e.printstacktrace ();}}

Output:

Database name: MySQL database version: 5.5.19 table Category: Jdbcdemo table schema: null table name: Users column name: ID PRIMARY key: 1 primary Key Name: PRIMARY

(2) resultsetmetadata:

package javase.jdbc;import java.sql.connection;import  Java.sql.preparedstatement;import java.sql.resultsetmetadata;import java.sql.sqlexception;public  class resultsetmetadatademo {public static void main (String[] args)  { string sql =  "Select id,username,password,classid from users"; Connection connection = jdbcconnection.getconnection ();try {preparedstatement  Pstatement = connection.preparestatement (SQL); Resultsetmetadata rsetmetadata = pstatement.getmetadata (); SYSTEM.OUT.PRINTLN ("Return Data has"  + rsetmetadata.getcolumncount ()  +  "Fields");if  ( Rsetmetadata.isautoincrement (1)) System.out.println (Rsetmetadata.getcolumnname (1)  +  "field is self-increasing");  catch  (sqlexception e)  {e.printstacktrace ();}} 

Output:

Return data has 4 field ID field is self-increment


This article is from "Zifangsky's personal blog" blog, make sure to keep this source http://983836259.blog.51cto.com/7311475/1767351

Using the metadata Analysis database in the Java Basic series 15:JDBC

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.