When daily, you often have a classmate to ask questions about how to connect to the database, now write down, I hope to have some people, some help.
^ _ ^ Is wrong, please leave your mouth ^ _ ^
1. Load a JDBC driver for a corresponding database
Before establishing a connection to a database, you must first load the JDBC driver of this database. This DRIVER will automatically register into the JDBC driver list after loading. There are two ways to load a JDBC driver.
a) Specify the drive in the command line mode or split the drive list:
The specific command is as follows:
C: /> java -djdbc.drivers = com.company1.driver: com.company2.driver youproject
b) The second method is called in the program.Forname () method. Recommended Use. . . .
Try
{
String drivername = "com.imaginary.sql.msql.msqldriver";
Class.Forname (Drivername) .newinstance ();
}
Catch (ClassNotFoundException E1)
{
// catch could not find Database Driver Exception.
}
2. Connect to the database.
There are different databases depending on the database to be connected in your back, and there is a small difference.
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 = "Datebase
1"
String username = "hello";
String Userpsw = "world";
String url = "jdbc: Oracle.thin: @" servername ":" serverport ":" ServerID;
Connection = DriverManager.getConnection (URL, UserName, Userpsw);
}
Catch (ClassNotFoundException E1)
{
// catch could not find Database Driver Exception.
}
Catch (SQLEXCEPTION E2)
{
// Catch Could Not Connect to the Database Exception.
}
b) Connect to a SQL Server database.
Connection connection = NULL;
Try
{
// load the JDBC Driver;
String drivername = "com.microsoft.jdbc.sqlserver.sqlserdriver";
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 could not find Database Driver Exception.
}
Catch (SQLEXCEPTION E2)
{
// Catch Could 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 could not find Database Driver Exception.
}
Catch (SQLEXCEPTION E2)
{
// Catch Could Not Connect to the Database Exception.
}
In fact, the three database connection methods are integrated. Due to the different databases and the database drivers used, there is a small difference in the surface of the code, but through the surface, it is
1. Load a specific database JDBC driver.
2. Connect to a database.
3. Thereafter, a specific database can be performed.
Attached to the JDBC driver of various databases:
http://java.sun.com/products/jdbc
For Oracle Database, please refer to:
http://otn.racle.com.software/content.html
For mysql database, please refer to:
http://mmmmysql.sourceforge.net
There are a lot of drive options for the SQL Server database, which is often used:
http://www.microsoft.com/china/sql/downloads/2000/jdbc.asp
http://www.freetds.org