Reads the names of all tables in the database.

Source: Internet
Author: User

 

The connection is simple, so I will not write it. Thanks for the main implementation, you can use the URL. If you want to see the connection, you can check the next one. Generally, I am not allowed to write it here.

Package cn.edu. nwsuaf. CIE. Aisa. WSC. operation;

Import java. SQL. connection;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. util. arraylist;
Import java. util. hashtable;
Import java. util. List;

Import com. MySQL. JDBC. resultsetmetadata;

Public class databaseattribute {
Connectionsource dbconnectionsourceimpl = NULL; // This is defined by an interface.
Resultset = NULL;
Public hashtable <string, Object> ROW = NULL;
Preparedstatement = NULL;
 
Public list <string> listattributelist = NULL;

Public list <string> getattribute (string tablenamestring ){
Dbconnectionsourceimpl = new dbconnectionsourceimpl ();
Connection connection = dbconnectionsourceimpl. getconnection ();
Try {
Preparedstatement = connection. preparestatement ("select * from"
+ Tablenamestring );
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Resultset = NULL;
Try {
Resultset = preparedstatement.exe cutequery ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

Resultsetmetadata metadata = NULL;
Try {
Metadata = (resultsetmetadata) resultset. getmetadata ();
} Catch (sqlexception E1 ){
// Todo auto-generated Catch Block
E1.printstacktrace ();
}
// Map <object, Object> attributemmap = NULL;
Listattributelist = new arraylist <string> ();
Try {
// While (resultset. Next ()){
For (INT I = 1; I <= metadata. getcolumncount (); I ++ ){
// Attributemmap = new hashmap <object, Object> ();
System. Out. println ("metadata. getcolumncount ()" + metadata. getcolumncount ());
System. Out. println (metadata. getcolumnname (I ));
Listattributelist. Add (metadata. getcolumnname (I ));
// Attributemmap. Put (metadata. getcolumnname (I), resultset. GetObject (I ));
}
// Listattributelist. Add (attributemmap );
//}
} Catch (sqlexception E1 ){
// Todo auto-generated Catch Block
E1.printstacktrace ();
}
Try {
Resultset. Close ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Try {
Preparedstatement. Close ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Try {
Connection. Close ();
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return listattributelist;

}
}

 

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.