Use JDBC to query the table's primary key

Source: Internet
Author: User
Recently, I have been working on restructuring database tables. Today I have encountered a related operation on getting the primary key of a [given table name] and performing other related operations. Public String [] gettablepk (string tablecodes) throws exception {<br/> string [] resultarray = NULL; <br/> connection conn = NULL; <br/> databasemetadata dbmd = NULL; <br/> resultset rs = NULL; <br/> try {<br/>/** check the API, the resultset contains five columns: <br/> * The table_cat table directory is generally null <br/> * table_schem table index. The structure of the table where the primary key is located, it is usually set to dbmd. getUserName () <br/> * table_name table name containing the primary key <br/> * column_name primary key name <br/> * key_seq master Key sequence <br/> * pk_name constraint name <br/> */<br/> conn = datasource. getconnection (); <br/> dbmd = Conn. getmetadata (); <br/> rs = DBMS. getprimarykeys (null, null, tablecodes); <br/> string temppk = ""; <br/> while (RS. next () {<br/> temppk = Rs. getstring ("column_name") + ","; <br/>}< br/> resultarray = temppk. split (","); <br/> If (temppk. length () <1) {<br/> resultarray = NULL; <br/>}< br/>} catch (sqlexc Eption sqle) {<br/> logger. Error (sqle); <br/>}finally {<br/> If (RS! = NULL) {<br/> try {<br/> Rs. close (); <br/>}catch (sqlexception sqle) {<br/> logger. error (sqle); <br/>}< br/> If (Conn! = NULL) {<br/> try {<br/>} catch (sqlexception sqle) {<br/> logger. error (sqle); <br/>}< br/> return resultarray; <br/>}< br/>

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.