Find the primary key of the data sheet

xiaoxiao2021-03-06  108

Most databases have many primary keys, but the same primary key of the two records is not allowed in a table has the same value. You can use Java Database Connectivity (JDBC) to determine the primary key of a data table. JDBC has powerful metadata processing capabilities. Java.sql.connection class and java.sql.resultset class can be reflected by calling its GetMetadata method, for example: // All classes in java.sql

Connection connection = ..... DatabaseMetadata dbmeta = connection.getMetadata (); resultset Rset = ..... ResultSetMetAdata Rsmeta = RSETMETMETADATA (); java.sql.databaseMetadata class contains a method of finding a data indicator key. You need to know the name of the table, the Catalog name and the Schema name. If you don't know Catalog and Schema, you can enter "NULL" without using them. For example: // Find a primary key of the table name "comment" // No Catalog or Schema, is set to NULL

ResultSet pkrset = dbmeta.getPrimaryKeys (NULL, NULL, "Comment"); while (pkrset.next ()) {system.rr.println ("************"); system. Err.Println ("Table_cat:" pkrSet.GetObject (1)); System.err.Println ("Table_SChem:" pkrset.getObject (2)); System.err.Println ("Table_Name:" pkrse.getObject (3)); system.err.println ("column_name:" pkrset.getObject (4)); system.err.println ("key_seq:" pkrset.GetObject (5)); system.rr.println (" PK_NAME: " pkrset.getObject (6)); system.err.println (" ************* ******** ");} In this example" Comment " Has a primary key called "comment_id". The following is the output of these code on MySQL: ****** Comment ****** Table_cat: Table_SChem: Table_name: CommentColumn_name: Column_idKey_SEQ: 1PK_NAME: Column_ID ****** ********* ******** ****** The reason for the presence of PK_NAME is sometimes the name other than the column name. KEY_SEQ represents the order of the primary key. Some databases that use alphabetical sequence save primary keys will return 0 to Key_SEQ. When creating a generic database app, the primary key for finding a table is basically. JDBC's Metadata class provides the required database reflex mechanism, making the implementation of these applications possible.

转载请注明原文地址:https://www.9cbs.com/read-125275.html

New Post(0)