JDBC application - database connection complete works

xiaoxiao2021-03-06  14

This is a basic article, mainly introduced

JDBC

In popular database

(SQL Server, Oracle)

Aspects of application. But it's all personal opinions, so I can't guarantee that it is completely correct, for reference only, if there is something different opinion, I can point out in the comments, I will make corresponding modifications.

:)

General case

,

Direct use

JDBC

Waiting for something

,

Compare the code of commonly used databases may be

: Private connection conn private string pool = ""; private string drivername = ""; private string user = "" private string pass = ""; private dbconnproxy proxy; //

Written

Class,

Control database access

public Object getConn () {try {Properties prop = new Properties (); prop.setProperty ( "user", user); prop.setProperty ( "password", pass); Driver mydriver = (Driver) Class.forName (driverName) .newinstance (); conn = mydriver.connect (poolurl, prop); //

or

CONN = MyDriver.Connect (PoolURL, NULL);

When is the special manager with me?

/ * Or: class.Forname (drivername); conn = drivermanager.getConnection (PoolURL, User, Pass); * /} catch (Exception E) {system.out.println ("Open Database Error / N" E.getMessage ());} If (! proxy.getdb ()) {this.close () Return Null;} Return Conn;} DRIVERNAME, POOLURL, USER, PASS can be obtained by reading an XML file or a normal attribute file. But even SQLServer is still Oracle, just change the configuration. They can be used in Sun.jdbc.odbc.jdbCodbcdriver. Corresponding to Pool = "JDBC: ODBC: Test", user, pass slightly can also be unique: sqlserver: weblogic.jdbc.msqlserver4.driver (online) corresponding poolurl = "JDBC: WebLogic: MSSQLServer4: CEIC@127.0.0.1 "// CEIC is the database name, 127.0.0.1 is a data source location, which can be a remote database, user, pass slightly oracle: Oracle.jdbc.driver.OracleDriver (Class12.zip, installed Oracle) corresponds to JDBC : Oracle: Thin: Scott/tiger@10.1.103.234: 1521: SP "Scott / Tiger connection identity username / password, 10.1.103.234, data source IP, 1521: Port (default is 1521). SP points to the database name User If the Pass is slightly used by the WebLogic database connection, it can pass CONTEXT CTX = New InitialContext (); DataSource DS = (DataSource) CTX.lookup ("ForceCDataSource"); // forceicDataSource is a matching DataSource. You can also pass and above Unified method to call. WebLogic: WebLogic.jdbc.pool.driver // drivername The corresponding poolURL = "JDBC: WebLogic: pool: poolname" // poolname is the name of the JDBC type in WebLogic .. IrsUser, Pass is the username and password of the WebLogic user (the user in the Administrators user group). (If not used with DriverManager, "conn = mydriver.connect (poolurl, null);" is in this case. I In WebLogic 6.1 test, it can be seen, it can be seen that most database connections can be flexible by configuring properties files. (Transplant is very convenient, and there is no impact on the application, of course, can also be boast Platform) So I can solve it by dynamically read attribute files. Private string javahome = system.getproperty ("JA Va.home "); private final string fs = system.getProperty (" file.separator "); private string path = javahome fs " db.properties "; // put db.properties under system properties Java.home, Generally is in the JRE directory under the JDK directory.

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

New Post(0)