Java parsing of database table names

Source: Internet
Author: User
Package cn.edu. nwsuaf. CIE. Aisa. WSC. operation;

Import java. SQL. connection;
Import java. SQL. resultset;
Import java. SQL. sqlexception;

Import com. MySQL. JDBC. databasemetadata;

Public class databasetablenameread {
Connectionsource dbconnectionsourceimpl = NULL;

Public resultset getdatabasetablenameread (){
Dbconnectionsourceimpl = new dbconnectionsourceimpl ();
Connection connection = dbconnectionsourceimpl. getconnection ();
Resultset = NULL;
Try {
Databasemetadata meta = (databasemetadata) connection. getmetadata ();
Resultset = meta. gettables (null,
New String [] {"table "});
While (resultset. Next ()){
System. Out. println ("table name:" + resultset. getstring (3 ));
System. Out. println ("Table Username:" + resultset. getstring (2 ));
System. Out. println ("name:" + resultset. getstring (1 ));
System. Out. println ("------------------------------");
}
Connection. Close ();
} Catch (exception e ){
Try {
Connection. Close ();
} Catch (sqlexception E1 ){
// Todo auto-generated Catch Block
E1.printstacktrace ();
}
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return resultset;
}
}

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

Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. sqlexception;

Import cn.edu. nwsuaf. CIE. Aisa. WSC. informationpushmain. informationpush;

/**
* @ Author ����������ڣ 7-7-14 ��ļ ļ ¼ ������
*/
Public class dbconnectionsourceimpl implements connectionsource {
Proopertiess properties = informationpush. properties;
@ Override
Public connection getconnection (){
// Todo auto-generated method stub
Connection connection = NULL;
If (informationpush. properties. getdatasource (). getdbtype (). Equals ("MySQL ")){
Try {
Class. forname ("com. MySQL. JDBC. Driver ");
} Catch (classnotfoundexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
String url = informationpush. properties. getdatasource (). geturl ();
String usename = informationpush. properties. getdatasource (). getuser ();
String Password = informationpush. properties. getdatasource (). GetPassword ();
Try {
System. Out. println (URL );
Connection = (connection) drivermanager. getconnection (URL,
Usename, password );
Return connection;
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
System. Out. println (E. getmessage ());
E. printstacktrace ();
}

} Else if (informationpush. properties. getdatasource (). getdbtype (). Equals ("sqlserver ")){

Try {
Class. forname ("com. Microsoft. sqlserver. JDBC. sqlserverdriver ");
} Catch (classnotfoundexception E1 ){
// Todo auto-generated Catch Block
E1.printstacktrace ();
}
String url = informationpush. properties. getdatasource (). geturl ();
String user = informationpush. properties. getdatasource (). getuser ();
String Pwd = informationpush. properties. getdatasource (). GetPassword ();
Try {

Connection = drivermanager. getconnection (URL, user, PWD );
Return connection;
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}

Else if (informationpush. properties. getdatasource (). getdbtype (). Equals ("oracle ")){

Try {
Class. forname ("oracle. JDBC. Driver. oracledriver ");
} Catch (classnotfoundexception E1 ){
// Todo auto-generated Catch Block
E1.printstacktrace ();
}
String url = informationpush. properties. getdatasource (). geturl ();
String user = informationpush. properties. getdatasource (). getuser ();
String Pwd = informationpush. properties. getdatasource (). GetPassword ();
Try {

Connection = drivermanager. getconnection (URL, user, PWD );
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
} Else if (informationpush. properties. getdatasource (). getdbtype (). Equals ("access ")){

Try {
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
} Catch (classnotfoundexception E1 ){
// Todo auto-generated Catch Block
E1.printstacktrace ();
}
String url = informationpush. properties. getdatasource (). geturl ();
String user = informationpush. properties. getdatasource (). getuser ();
String Pwd = informationpush. properties. getdatasource (). GetPassword ();
Try {
Connection = drivermanager. getconnection (URL, user, PWD );
Return connection;
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

}
Return connection;
}
}

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

Import java. SQL. connection;

/**
* @ Author
* ����ڣ 7-14
*/
Public interface connectionsource {

Public connection getconnection ();

}

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.