JNDITechnology andJDBCSince the combination of technologies has so many advantages, what should we do?RowsetPackage
Provides this functionJNDITechnology andJDBCThe principles of technology integration are as follows:
UseJDBCAndJNDITechnology, write a component that provides database services, and then publish it to a certain
In the inter-layer server, listen for service requests. Next we can writeJavaApplication search provides database services
Once you find it, you can use the methods it provides to perform specific database operations.
RowsetPackage-definedDatasourceInterface to establish a database connectionJNDIYes. The specific method is as follows:
1. CreateJavaClassSampledatasourceThis class has been extendedJavax... SQL. datasourceInterface
For more information about how to obtain the database connection function, see the program list.
ProgramSampledatasource. Java is as follows:
Package example6;
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. sqlexception;
Public class sampledatasource implementsjavax. SQL. datasource,
Javax. Naming. referenceable, java. Io. serializable {
/* Constructors */
Publicsampledatasource (){
// This constructor is needed by the object Factory
}
/** Properties */
Publicstring getservername (){
Returnservername;
} Public void setservername (stringservername ){
This. servername = servername;
}
Publicstring getdatabasename (){
Returndatabasename;
}
Publicvoid setdatabasename (string databasename ){
This. databasename = databasename;
}
/*
* Methods inherited from datasource
*/
Publicconnection getconnection () throws sqlexception {
// Vendor Specific code to create a jdbcconnection goes here
Try {
Class. forname ("jdbcdriver ");
Connectionconn = drivermanager. getconnection ("jdbcurl", "user ",
"Pass ");
Returnconn;
} Catch (exception Fe ){
// To do nothing
}
Returnnull;
}
Publicconnection getconnection (string username, string password)
Throwssqlexception {
// Vendor Specific code to create a jdbcconnection goes here
Stringuser = username;
Stringpass = password;
Try {
Class. forname ("jdbcdriver ");
Connectionconn = drivermanager
. Getconnection ("jdbcurl", user, pass );
Returnconn;
} Catch (exception Fe ){
// To do nothing
}
Returnnull;
}
Publicjava. Io. printwriter getlogwriter () throws sqlexception {
// Vendor Specific Code goes here
Return NULL;
}
Publicvoid setlogwriter (Java. Io. printwriter out) throws sqlexception {
// Vendor Specific Code goes here
}
Publicvoid setlogintimeout (INT seconds) throws sqlexception {
// Vendor Specific Code goes here
}
Publicint getlogintimeout () throws sqlexception {
Return1;
// Vendor Specific Code goes here
}
Privatestring servername = NULL;
Privatestring databasename = NULL;
}
InSampledatasourceThe. Java class definesGetconnection() This method can be used to obtainJava. SQL. ConnectionInterface instance object.
(2) Next, we should writeJavaApplication.Sampledatasource
The component is published to the intermediate layer server. Assume that this application is named JND.IEXam. Java, it should contain
The following code snippet.
Example
Sampledatasource SDS = newsampledatasource ();
SDS. SetServerName ("Rainbow ");
SDS. setdatabasename ("fancy ");
Context CTX = new initialcontext ();
CTX. BIND ("JDBC/employeedb" SDS );
In the above Code, the first line createsSampledatasourceObject, the next two lines of code are called separately
UseSetservername() Method andSetdatabasename() Method, initializationSampledatasourceObject Attributes
Then createJNDIName the environment object CTX and set the sampleDatasourceClass Instance Object SDS boundJNDIJDBC/employeedb.
Compile and run JNDIEXam. Java program, SetJNDIPublish the service. In addition to using this method, you can also
Visual publishing using some gui toolsJNDIService.
(3) Next, we should write a client (this refers to the relative client, a server is relative to another
For the server, it can be a server or a client)JavaApplications, suchJSPProgram to useJNDIService, please refer to the followingJSPCode segment (the try/catch module has been omitted ).JSPFor more information about the code, see Section 6.4.
Example:
<%
Context CTX = new initialcontext ();
DatasourceDS = (Datasource) CTX. Lookup ("JDBC/employeedb ");
ConnectionCon = Ds. GetConnection(JDBC/webdatabase "" sa ");
Con. setautocommit (false );
StatementStmt = con. CreateStatement();
ResultsetRs = stmt.Executequery("Select * from goods ");
While (Rs. Next ())
{
Out. println (Rs. getstring (1 ));
}
Con. Commit ();
Con. Close ();
%>