Java Connection Database

Source: Internet
Author: User

In the process of software development, it is necessary to deal with the database. The Java connection database is relatively simple, but for the people who are just beginning to learn programming, may be a little bit unknown from where to start, now for you to explain the Java Connection database method:

1. Open MyEclipse, create a new Java project;

Create a new package under the project;

2. Create a new Java class, called Dbmanger, and write code in this class:

After the code is written, add the driver package to the database;

Right-click on the project----build path----Configure Build path---Click Libraries

Click Add External JARS ....

Locate the appropriate database driver package (this is to download the corresponding database driver on the web)

  

Click OK, you can add it in;

Full Java code:

Package com.test.db;
The package introduced
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;

public class Dbmanger {
public static void Main (string[] args) {

try{
Loading a database driver with Class.forName
Class.forName ("Oracle.jdbc.driver.OracleDriver");
Get the database to connect
Connection conn=drivermanager.getconnection ("Jdbc:oracle:thin:@127.0.0.1:1521:orcl",
"Scott", "admin123");
Determine if the database connection is non-empty
if (conn!=null) {
SYSTEM.OUT.PRINTLN ("Database connection succeeded");
}

}
catch (ClassNotFoundException e) {
E.printstacktrace ();
}
catch (SQLException e) {
E.printstacktrace ();
}
}
}

Java Connection Database

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.