JDBC Connection Database Experience Tips

xiaoxiao2021-03-06  33

source:

http://blog.9cbs.net/legenDinfo/archive/2005/01/24/265624.aspx

Java Database Connections (JDBC) consists of a set of classes and interfaces written in Java programming languages. JDBC provides a standard API for tools / database developers, enabling them to write database applications with pure Java APIs. However, the interfaces of the developers are not exactly the same, so the development environment changes will bring a certain change. This article mainly gathers different databases of different databases.

First, connect various database mode quick check table

Below, various databases use the JDBC connection, can be used as a manual.

1, Oracle8 / 8i / 9i database (Thin mode)

Class.Forname ("Oracle.jdbc.driver.OracleDriver"). NewInstance (); String Url = "JDBC: Oracle: Thin: @localhost: 1521: ORCL"; // ORCL is the sid string user = "test" String password = "test"; connection conn = drivermanager.getConnection (URL, User, Password);

2, DB2 database

Class.Forname ("com.ibm.db2.jdbc.app.db2driver") .newinstance (); string url = "jdbc: db2: // localhost: 5000 / sample"; // Sample for your database name String User = "Admin"; string password = ""; connection conn = drivermanager.getConnection (URL, User, Password);

3, SQL Server7.0 / 2000 database

Class.Forname ("com.microsoft.jdbc.sqlser.sqlserverdriver). NewInstance (); string url =" JDBC: Microsoft: SQLServer: // localhost: 1433; databasename = mydb "; // mydb is database string user = "sa"; string password = ""; connection conn = drivermanager.getConnection (URL, User, Password);

4, Sybase database

Class.Forname ("com.sybase.jdbc.sybdriver). Newinstance (); string url =" jdbc: sybase: tds: localhost: 5007 / mydb "; // mydb for your database name Properties Sysprops = System.getProperties (); Sysprops.put ("user", "userid"); sysprops.put ("password", "user_password"); connection conn = drivermanager.getConnection (URL, Sysprops);

5, Informix database

Class.forName ( "com.informix.jdbc.IfxDriver") newInstance (); String url = "jdbc:. Informix-sqli: //123.45.67.89: 1533 / myDB: INFORMIXSERVER = myserver; user = testuser; password = testpassword "; // mydb is the database name connection conn = drivermanager.getConnection (URL); 6, MySQL database

Class.forName ( "org.gjt.mm.mysql.Driver") newInstance (); String url =. "Jdbc:? Mysql: // localhost / myDB user = soft & password = soft1234 & useUnicode = true & characterEncoding = 8859_1" // myDB database Name Connection CONN = DriverManager.getConnection (URL);

7, PostgreSQL database

Class.Forname ("org.postgreSql.driver). NewInstance (); string url =" jdbc: postgreSQL: // localhost / mydb "// mydb is the database name string user =" myuser "; string password =" mypassword " Connection conn = drivermanager.getConnection (URL, User, Password);

8, Access database straight with ODBC

Class.Forname ("sun.jdbc.odbc.jdbcodbcdriver"); string url = "JDBC: ODBC: driver = {Microsoft Access Driver (* .mdb)}; dbq =" Application.getRealPath ("/ Data / ReportDemo. MDB "); connection conn = drivermanager.getConnection (URL,", "" "; statement stmtnew = conn.createstatement ();

Second, JDBC connection mysql method

Here is a small tutorial to connect Mysql using JDBC

1, find drivers

MySQL currently provides the Java driver for Connection / J, you can download from the mysql official website, and find the mysql-connector-java-3.0.15-ga-bin.jar file, this driver is a pure Java driver, no need to do Other configurations.

2, dynamically specify ClassPath

If you need to perform the ClassPath when you need to execute, use the -cp mode when executed. Otherwise add the above .jar file to the ClassPath environment variable.

3, load the driver

Try {class.forname (com.mysql.jdbc.driver); system.out.println (Success Loading MySQL DRIVER!);} catch (Exception E) {system.out.println (Error Loading MySQL Driver!); E. PRINTSTACKTRACE ();

4, set the connected URL

JDBC: mysql: // localhost / databasename [? PA = VA] [& pa = va]

Third, the following is listed below to use some of the techniques that can be used when using JDBC to connect to the Oracle database, using the Thin driver in the client software development In developing Java software, Oracle's database provides four types of drivers, two For client software such as applications, applets, servlets, and two server-side software such as Java stored procedures in the database. In the development of client-end software, we can select an OCI driver or Thin driver. The OCI driver uses Java localization interface (JNI) to communicate with the database through Oracle client software. The Thin driver is a pure Java driver that communicates directly with the database. In order to achieve the highest performance, Oracle recommends using an OCI driver in the development of client software, which seems correct. But I recommend using Thin drivers because it is discovered by multiple tests, in general, the performance of the Thin driver exceeds the OCI driver. 2. Turn off the automatic submission function, improve system performance When the connection with the database is established for the first time, the connection is in automatic submission mode by default. In order to get better performance, you can turn off the automatic submission function by calling the setOCommit () method of the Connection class with Pooler FALSE parameters, as shown below: Conn.setAutocommit (false); It is worth noting that automatic submission function is closed We need to manage transactions by calling the COMMIT () and rollback () methods to call the CONNECTION class. 3. When using the Statement object in dynamic SQL or time-limited commands, we have two options: You can use the PreparedStateMent object, or you can use the Statement object. No matter how many times use the same SQL command, PreparedStatement is only parsed and compiled once. When using the Statement object, it is parsed and compiled each time a SQL command is executed. This may make you think that using the PreparedStateMent object is faster than using the Statement object. However, the test I conducted indicated that in the client software, the situation is not the case. Therefore, in the SQL operation with time-limited, we should consider using the Statement object unless divided by the SQL command. In addition, using the Statement object also makes the writing dynamic SQL commands easier because we can connect strings together to create a valid SQL command. So I think the Statement object can make the creation and execution of the dynamic SQL command easier. 4. When using the Helper function to format the dynamic SQL command to create a dynamic SQL command executed using the Statement object, we need to handle some formatted issues. For example, if we want to create a SQL command to insert the name O'Reilly into the table, you must use the "''" number of O'Reilly in two. Completing the best way to do this is to create a Helper method that completes the replacement operation, and then uses a SQL command when connecting a string to express a SQL command. Similarly, we can let the Helper method accept a DATE-type value and then allow it to output a string expression based on Oracle to_date () function. 5. When using the PreparedStatement object to improve the overall efficiency of the database When using the PreparedStateMent object, the command is parsed and compiled by the database, and then placed in the command buffer.

Then, whenever the same preparedStatement object is executed, it will be resolved once, but it will not be compiled again. A pre-compiled command can be found in the buffer and can be reused. In an enterprise application with a large number of users, the same SQL commands are often repeated, and the reduction in the number of compilations that use the PreparedStatement object can improve the overall performance of the database. If you are not created, prepared, executing the pre previoustement task is longer than the Statement task, and I will recommend using the PreparedStateMent object in all situations other than the dynamic SQL command. 6. Using the PreparedStatement object in a batch repetition insertion or update operation, if the insertion and update operation is processed, it is possible to significantly reduce the time they need. Statement and CallableStatement provided by Oracle do not really support batch, only PreparedStatement objects really support batch. We can use Addbatch () and Executebatch () methods to select a standard JDBC batch, or select the faster Oracle proprietary approach by using the setEcuteBatch () method of the PreparedStatement object and the standard executeUpdate () method. To use the Oracle proprietary batch mechanism, you can call setExecutebatch () as follows:

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

New Post(0)