Java connection data

xiaoxiao2021-03-06  22

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.

1. The JDBC driver loaded with a corresponding database must first load the JDBC driver of this database before loading, and this drive is automatically registered 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 the class.forname () method in the program. 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 = "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 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.

} The three database connection methods are integrated, in fact, the same size. 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.

转载请注明原文地址:https://www.9cbs.com/read-39767.html

New Post(0)