Connection Methods for four common databases: Mysql, Oracle, SQLserver, and ACCESS

Source: Internet
Author: User
Here, we mainly use some commonly used databases, and paste these items here to facilitate future queries. The network can be used as a reference. ** Four Common Database Connection Methods * publicclassSQLMode {** oracle driver connection method: Port Number 1521 * privatestaticStringOracleDri

Here, we mainly use some commonly used databases, and paste these items here to facilitate future queries. The network can be used as a reference. /** Four Common Database Connection Methods */public class SQLMode {/** oracle driver connection method: Port Number 1521 */private static String OracleDri

Here, we mainly use some commonly used databases, and paste these items here to facilitate future queries. The network can be used as a reference.

/** Four Common Database Connection Methods */
Public class SQLMode {
/** Oracle driver connection method: Port Number 1521 */
Private static String OracleDriver = "Oracle. jdbc. Driver. OracleDriver ";
Private static String url = "jdbc: Oracle: thin: @ localhost: 1521: mydata ";

/** MySQL driver connection method: Port Number 3306 */
Private static String MysqlDriver = "com. mysql. jdbc. Driver ";
Private static String url2 = "jdbc: mysql:/localhost: 3306/mydata ";
/** SQL server2005 DRIVER: Port Number 1433 */
Private static String sqlDriver = "com. microsoft. jdbc. sqlserver. SQLServerDriver"; // Method for connecting to the SQL database
Private static String url3 = "jdbc: microsoft: sqlserver: // localhost: 1433;" +
"DatabaseName = mydata ";
/* JDBC-ODBC access database */
Private static String accessDriver = "sun. jdbc. odbc. JdbcOdbcDriver ";
Private static String url4 = "jdbc: odbc: mydata"; // The system prompts you to configure access to load the data source.
// Open control panel --> performance and maintenance --> management tools --> data source (ODBC) --> Add
Private static String user = "admin ";
Private static String pwd = "password ";
/** Get Access connection */
Public static Connection getAccessCon (){
Try {
Class. forName (accessDriver );
Connection oracle = DriverManager. getConnection (url4, user, pwd );
Return oracle;
} Catch (Exception e ){
E. printStackTrace ();
Throw new RuntimeException (e );
}
}
/** Obtain the SQL Server connection */
Public static Connection getsqlSerCon (){
Try {
Class. forName (sqlDriver );
Connection sqlSer = DriverManager. getConnection (url3, user, pwd );
Return sqlSer;
} Catch (Exception e ){
E. printStackTrace ();
Throw new RuntimeException (e );
}
}
/** Obtain the Oracle connection */
Public static Connection getOracleCon (){
Try {
Class. forName (OracleDriver );
Connection oracle = DriverManager. getConnection (url, user, pwd );
Return oracle;
} Catch (Exception e ){
E. printStackTrace ();
Throw new RuntimeException (e );
}
}

/** Obtain mysql connection */
Public static Connection mysqlCon (){
Try {
Class. forName (MysqlDriver );
Connection mysql = DriverManager. getConnection (url2, user, pwd );
Return mysql;
} Catch (Exception e ){
Throw new RuntimeException (e );
}
}

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.