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