Database access is a very important part of database application system. As a common database management system, the Da Meng database provides a variety of database access interfaces, including ODBC, JDBC, API, OLE DB, and embedded methods. This article mainly lists the common way to connect to the dream database in Java ...
1. Establish a basic JDBC connection
JDBC (Java Database Connectivity) is the interface specification for Java applications and databases, designed to allow database developers to provide standard database application programming interfaces (APIs) for Java programmers. JDBC defines a common SQL database API across databases and across platforms. DM JDBC 3.0 driver complies with Sun JDBC3.0 Standard, compatible DM JDBC 2.0.
The DM jdbc Driver is a JDBC driver for the DM database, a general-purpose low-level application programming interface that supports basic SQL functionality, and supports general SQL database access.
To establish a JDBC connection, you first register the database driver. You can explicitly register the driver by calling the Registerdriver method of the Java.sql.DriverManager class, or you can register the driver implicitly by loading the database driver class.
//显示注册
DriverManager.registerDriver(newdm.jdbc.driver.dmDriver());
//隐式注册
Class.forName(“dm.jdbc.driver.DmDriver”);
The class that implements the Java.sql.Driver is loaded during implicit registration, and there is a static code snippet in the class that registers the class with the driver manager DriverManager during class loading. The code snippet for this static execution is actually the above explicitly registered code.
After registering the driver, you can call the driver Manager's Getconnection method to establish the connection. Establishing a database connection requires specifying a URL that marks a different database, the username user and password password to log on to the database.
The specific process of establishing a connection through DriverManager, as in the following example:
String driver= "dm.jdbc.driver.DmDriver";
String url= "jdbc:dm://localhost:12345/dbname";
String username="username";
String password="password";
Connection con = null;
try {
// 加载JDBC驱动程序
Class.forName(driver);
} catch (java.lang.ClassNotFoundException e) {
e.printStackTrace();
}
try {
// 数据库连接
con = DriverManager.getConnection(url, username, password);
} catch (SQLException ex) {
ex.printStackTrace();
}