Using JNDI technology in JDBC API ------ actual technology articles

xiaoxiao2021-03-06  40

JNDI technology provides a program operating mode such that Java applications can discover and request services on remote computing devices, such as database services. Here we focus on how JNDI technology combines JDBC technology to build a powerful Java application for accessing the database. What is the betterness of JNDI technology and JDBC technology? 1 Apply JNDI technology to implement real distributed processing, database server, components that provide database connection services, and execute database operations Java applications can be distributed in different addresses. Heterogeneous computing equipment. 2 Combine JNDI technology with JDBC technology to realize the function of database connection buffer pool. 3 Combine JNDI technology with JDBC technology can block a lot of cumbersome code to establish a database connection in the Java application, so that the security ROWSET package that helps the system provides this principle of functional JNDI technology and JDBC technology. As follows: Use JDBC and JNDI technology to write a component that provides database services and publish it into a middle-level server, listens for service requests. Next we can write a Java application to find components that provide database services. Once you find it (components), you can use the various methods it provides to perform specific database operations. The DataSource interface defined by the ROWSET package provides JNDI support for establishing a database connection.

The specific method is as follows: 1 First create a Java class SampleDataSource, which extends the javax..sql.datasource interface specifically implements the function of obtaining the database connection, the code of this class is as follows: // File Name SampleDataSource.java//author fancy // Date 2001.3.4 // Note to create tht datasource connectionimport java.sql *;. import javax.sql *;. import javax.naming *;. public class SampleDataSource implements javax.sql.DataSourcejavax.naming.Referenceable java.io .Serializable {/ * Constructors * / public SampleDataSource () {// This constructor is needed by the object factory} / ** Properties * / public String getServerName () {return serverName;} public void setServerName (String serverName) {this. serverName = serverName;} public String getDatabaseName () {return databaseName;} public void setDatabaseName (String databaseName) {this.databaseName = databaseName;} / ** Methods inherited from DataSource * / public Connection getConnection () throws SQLException {// vendor Specific code to create a JDBC Connection Goes Heretry {Class.Forname ("JDBCDRIVER"); connection conn = drivermanager.getConnection ("jdbcurl" "pass"); Return ConN n;} catch (Exception fe) {// to do nothing} return null;} public Connection getConnection (String username String password) throws SQLException {// vendor specific code to create a JDBC Connection goes hereString user = username; String pass = Password; try {class.forname ("jdbcdriver"); connection conn = drivermanager.getConnection ("JDBCURL" User Pass); return conn;} catch (Exception Fe) {// to do not do not do not do not do not

} Public java.io.PrintWriter getLogWriter () throws SQLException {// vendor specific code goes here} public void setLogWriter (java.io.PrintWriter out) throws SQLException {// vendor specific code goes here} public void setLoginTimeout (int seconds) throws SQLException {// vendor specific code goes here} public int getLoginTimeout () throws SQLException {// vendor specific code goes here} private String serverName = null; private String databaseName = null;} SampleDataSource class defined in the getConnection () method Using this method, you can get an instance object of the java.sql.connection interface. In addition to the getConnection () method, the SampleDataSource.java program also defines several other related methods. 2 Next, we should write a Java application to publish the above SampleDataSource component to the intermediate layer server, assume that this app is named JNDIEXAM.JAVA, then it should contain the following code segment Sample SampleDataSource SDS = New SampleDataSource (); SDS.SetServerName ("rainbow"); SDS.SetDatabaseName ("fancy"); context ctx = new initialContext (); ctx.bind ("JDBC / Employeedb" SDS); the first line of the above code is created A SampleDataSource object, the next two lines of code calls the setServerName () method and setDatabaseName () method to initialize the properties of the SampleDataSource object, then create the JNDI Name Environment object CTX, bind the instance of the SampleDataSource class to JNDI name JDBC / EMPLOYEDB in. Compile the JNDIEXAM.JAVA program to publish the JNDI service. In addition to using this method, you can also use certain GUI tools to visualize the publishing JNDI service. 3 Next We should write a client (here, the relative client, a server can be a server, such as a JSP program, etc., such as a JSP program, etc. relative to another server. JNDI service.

Please see the JSP code segment below (TRY / CATCH module is omitted) example <% context ctx = new initialcontext (); DataSource DS = (DataSource) CTX.lookup ("JDBC / Employeedb"); connection con = ds.getConnection (JDBC / WebDatabase "" SA "" "); con.setautocommit (false); statement stmt = con.createstatement (); resultset = stmt.executeQuery (" SELECT * from goods "); while (rs.next () ) {Out.println (rs.getstring (1));} Con.commit () ;con.close ();%> Program explanation http://blog.9cbs.net/jgsfy/archive/2005/ 01/04

/239800.aspx

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

New Post(0)