Java application Jndi method of using WebLogic connection pool

Source: Internet
Author: User
Tags getmessage stmt

The first step is to make it clear that to get a weblogic connection you have to rely on the weblogic associated jar, Not weblogic.jar on the Internet, if you put Weblogic.jar in your classpath is not enough, because the Java class file can not find errors such as [caused by: Weblogic.security.subject.AbstractSubject

Java.lang.noclassdeffounderror:weblogic/security/subject/abstractsubject] exception. So what do we do, in fact, it's very simple. Enter into the Server/lib directory under your Webloigc_home path and run the [jdk1.6 command java-jar wljarbuilder.jar "jdk1.5 command" Java-jar Wljarbuilder.jar-profilewlfullclient5 "], copy" Wlfullclient.jar or Wlfullclient5.jar "to your classpath environment;
The second coding work: Create a new Java project, a new Java class, named Connector.java; First we initialize the environment to get the database connection, that is, initialize the context;

Hashtable<string, String>ht=new hashtable<string, string> ();
Ht.put (context.initial_context_factory,initial_context_factory);
Ht.put (Context.provider_url, Provider_url);
try {
Long T1=system.currenttimemillis ();
_context=new InitialContext (HT);
Long T2=system.currenttimemillis ();
System.out.println ("Initial the context Cost:" + (T2-T1) + "millseconds!");
catch (Namingexception e) {
System.out.println ("Initial context failured Withexception:" +e.getmessage ());
E.printstacktrace ();
}

Next, look for JNDI from the context to get to the DataSource object: Javax.sql.DataSource ds= (Javax.sql.DataSource) ctx.lookup (JNDI);
And then from the DataSource to get Java.sql.Connection object. Ds.getconnection ();

3 Specific sample code please refer to the following content, the author of the source is fully posted:

Import java.sql.*;
Import java.util.*;
Import javax.naming.*;
/**
* <pre>
* To get the connections of WebLogic connection pool by JNDI
* The required jars include "Wlfullclient.jar",
* I know you'll ask me how to get the This jar? The following is the answer:
* Must has installed the weblogic,wl_home means the WebLogic setupdirectory.
* (a). Creating a Wlfullclient.jar for JDK 1.6 client applications
* ===================================================================
* 1.Change directories to the Server/lib directory.
* CD Wl_home/server/lib
* 2.Use The following command to create Wlfullclient.jar inthe "server/lib" directory:
* Java-jar Wljarbuilder.jar
* This may take a few minutes, just wait!
* 3.You can now copy and bundle the Wlfullclient.jar withclient applications.
* 4.Add the Wlfullclient.jar to the client application ' s classpath.
* ===================================================================
* (b). Creating a Wlfullclient5.jar for JDK 1.5 client applications
* ===================================================================
* 1. The same as (a). 1
* 2. Use the following command to create Wlfullclient.jar inthe Server/lib directory:
* Java-jar WLJARBUILDER.JAR-PROFILEWLFULLCLIENT5
* 3. The same as (a). 3
* 4. The same as (a). 4
* ===================================================================
*
*
* </pre>
* @author Ubuntu
*
*/
public class Connector {
/**
* The const variable of initial context factory,this can ' t modified!
*/
Final static String initial_context_factory= "Weblogic.jndi.WLInitialContextFactory";
/**
* The const variable of the T3 provider Url,this need to modified by your own!
*/
Final static String provider_url= "T3://10.254.0.9:7001″;

/**
* The const variable of the JNDI name, this is need to modified by your own!
*/
Final static String jndi= "Jdbc/powererp";

/**
* The method so get Connecton from WebLogic JNDI
* @return The Connection Object
*/
Public Connection getconnection () {
Context Ctx=null;
Connection Con=null;
try{
Long T1=system.currenttimemillis ();
Ctx=_context;
Long T2=system.currenttimemillis ();
System.out.println ("Initial the context Cost:" + (T2-T1) + "millseconds!");
Javax.sql.DataSource ds= (Javax.sql.DataSource) ctx.lookup (JNDI);
Long T3=system.currenttimemillis ();
System.out.println ("Get the DataSource Cost:" + (T3-T2) + "millseconds!");
Con=ds.getconnection ();
Long T4=system.currenttimemillis ();
System.out.println ("Get the Connection Cost:" + (T4-T3) + "millseconds!");

}catch (Exception e) {
E.printstacktrace ();
}
return con;
}

static context _context=null;

static{
Hashtable<string, string> ht=new hashtable<string, string> ();
Ht.put (Context.INITIAL_ Context_factory,initial_context_factory);
Ht.put (Context.provider_url, Provider_url);
try {
Long t1=system.currenttimemillis ();
_context=new InitialContext (HT);
Long t2= System.currenttimemillis ();
System.out.println ("Initial the context Cost:" + (T2-T1) + "millseconds!");
} catch (Namingexception e) {
System.out.println ("Initial context failured Withexception:" +e.getmessage ());
E.printstacktrace ();
}
}

/**
* The method This test whether the connection is correct got
* @param con
*/
private void Testconn (Connection con) {
ResultSet Rs=null;
Statement Stmt=null;
String sql= "Select sysdate from Dual";
if (con==null) throw new RuntimeException ("The Connectionis null.please check!");
try {
Stmt=con.preparestatement (SQL);
Rs=stmt.executequery (SQL);
if (Rs!=null&&rs.next ()) {
String sysdate=rs.getstring (1);
SYSTEM.OUT.PRINTLN ("The Oracle DB Sysdate is:" +sysdate);
}
catch (SQLException e) {
E.printstacktrace ();
throw new RuntimeException ("The Excepiton occured:" +e.getmessage ());

}finally{
try{
if (rs!=null) rs.close ();
if (stmt!=null) stmt.close ();
}
catch (Exception e) {}
}

}

/**
* Test the connection by Jndi
*/
private void Invoketestdbconnectionbyjndi () {
Connection Con=null;
try{
Long Tstart=system.currenttimemillis ();
Con= getconnection ();
Long Tend=system.currenttimemillis ();
System.out.println ("Execute" getconnection () Cost: "+ (Tend-tstart) +" millseconds! ");
Testconn (con);
}catch (Exception e) {
E.printstacktrace ();
throw new RuntimeException ("When Executeinvoketestdbconnectionbyjndi ()," +
"The Excepiton occured:" +e.getmessage ());
}finally{
try{
if (con!=null) con.close ();
}
catch (Exception e) {}
}

}

/**
* The Main method
* @param args args allow null
*/
public static void Main (string[] args) {
Connector cct=new Connector ();
Cct.invoketestdbconnectionbyjndi ();
Cct.invoketestdbconnectionbyjndi ();
}

—————-thefollowing his-the test Result: ———————//
/**
*
* Initial the context cost:5666 millseconds!
* Get the DataSource cost:225 millseconds!
* Get the connection cost:968 millseconds!
* Execute the Method getconnection () cost:1193 millseconds!
* The Oracle DB sysdate is:2010-08-24 13:31:50.0
* Get the DataSource cost:4 millseconds!
* Get the connection cost:33 millseconds!
* Execute the Method getconnection () cost:37 millseconds!
* The Oracle DB sysdate is:2010-08-24 13:31:51.0
*
*/
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.