In daily work, some people often ask questions about how to connect to the database. Now I want to write it down and help some people.
1. Load a JDBC driver for the corresponding database
Before establishing a connection to a database, you must first load the JDBC driver of the database. After loading, the driver will be automatically registered to the JDBC driver list. There are two methods to load a JDBC driver.
A) Specify the drive in the command line mode or use a colon to split the drive list:
The command is as follows:
C:/> JAVA-djdbc. Drivers = com. company1.driver: COM. company2.driver youproject
B) Method 2: Call the class. forname () method in the program. It is recommended to use ....
Try
{
String drivername = "com. Imaginary. SQL. msql. msqldriver ";
Class. forname (drivername). newinstance ();
}
Catch (classnotfoundexception E1)
{
// Catch cocould not find database driver exception.
}
2. Connect to the database.
There is a small difference between the databases to be connected in the background.
A) connect to the Oracle database.
Connection connection = NULL;
Try
{
// Load the JDBC driver;
String drivername = "oracle. JDBC. Driver. oracledriver ";
Class. forname (drivername). newinstance ();
// Create a connection to the database;
String servername = "127.0.0.1 ";
String SERVERPORT = "1521 ";
String serverid = "datebase1"
String username = "hello ";
String userpsw = "world ";
String url = "JDBC: Oracle. Thin: @" + servername + ":" + SERVERPORT + ":" + serverid;
Connection = drivermanager. getconnection (URL, username, userpsw );
}
Catch (classnotfoundexception E1)
{
// Catch cocould not find database driver exception.
}
Catch (sqlexception E2)
{
// Catch cocould not connect to the database exception.
}
B) connect to an SQL Server database.
Connection connection = NULL;
Try
{
// Load the JDBC driver;
String drivername = "com. Microsoft. JDBC. sqlserver. sqlserverdriver ";
Class. forname (drivername). newinstance ();
// Create a connection to the database;
String servername = "127.0.0.1 ";
String SERVERPORT = "1433 ";
String serverid = servername + SERVERPORT;
String username = "hello ";
String userpsw = "world ";
String url = "JDBC: jsqlconnect: //" + serverid;
Connection = drivermanager. getconnection (URL, username, userpsw );
}
Catch (classnotfoundexception E1)
{
// Catch cocould not find database driver exception.
}
Catch (sqlexception E2)
{
// Catch cocould not connect to the database exception.
}
C) connect to a MySQL database ....
Connection connection = NULL;
Try
{
// Load the JDBC driver;
String drivername = "org. gjt. Mm. MySQL. Driver ";
Class. forname (drivername). newinstance ();
// Create a connection to the database;
String servername = "127.0.0.1 ";
String serverid = "Database ";
String username = "hello ";
String userpsw = "world ";
String url = "JDBC: mysql: //" + servername + "/" + serverid;
Connection = drivermanager. getconnection (URL, username, userpsw );
}
Catch (classnotfoundexception E1)
{
// Catch cocould not find database driver exception.
}
Catch (sqlexception E2)
{
// Catch cocould not connect to the database exception.
}
The above three database connection methods are similar. Because different databases are accessed and the database drivers used are different, the Code may be slightly different on the surface, but on the surface, internal
1. Load a specific database JDBC driver.
2. connect to a database.
3. Then, you can perform specific operations on a specific database.