Java connection to the database

Source: Internet
Author: User

Java connection to the database

A few notes about connecting Java to the database

1. connect java to the Oracle database

Use the following code to perform three steps:

1. Download The ojdbc. jar package and import it to the project. Appendix: http://download.csdn.net/detail/zy5669536/2606669

2. Place the following code where you think it should be.

3. modify the code: Change MyDbComputerNameOrIP to the IP address of the computer where the Oracle database is located, and change UserName and Password to the user name and Password defined earlier.

Then, you can use it.

Connection connect = null; Class. forName ("oracle. jdbc. driver. oracleDriver "); // Add the oracle Driver." "contains the driver path String url =" jdbc: oracle: thin: @ MyDbComputerNameOrIP: 1521: myorcl "; // database connection. oracle indicates connecting to the oracle database; thin: @ MyDbComputerNameOrIP indicates the IP address of the database (thin can be retained :); 1521 indicates the port number of the connected database; myorcl indicates the database name.
String UserName = "*******"; // Database User Login Name
String Password = "*******"; // Password
Connect = DriverManager. getConnection (url, UserName, Password );

2. connect Java to the MySQL database

Use the following code to perform three steps:

1. Download The mysql-jdbc.jar package and import it to the project. Appendix: http://download.csdn.net/detail/u010032692/5180868

2. Keep the following code where you think it should be.

3. modify the code: Change localhost to the IP address of the computer where the MySQL database is located, change test to the database name, and change root to the user name and password defined earlier.

Then, you can use it.

Class. forName ("com. mysql. jdbc. driver "); Connection connect = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/test", // "root", // username "root" // password );

 

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.