Java learning: JNDI and JDBC connection tests under WebLogic (WebLogic environment)

Source: Internet
Author: User

For the professional explanation of JNDI, you can search for it on your own.

From the perspective of usage, we can simply regard it as a "hash resource" container of key-value. Given a string type key, you can put any type of value into this container (using the bind/rebind method); when you need to use this resource elsewhere, the resource can be retrieved based on the key (using the lookup method)

JNDI usage example:

Package jmyang. webLogic;/*** <p> title: JNDI example (WebLogic environment) </P> * <p> Description: </P> * <p> copyright: copyright (c) 2012 </P> * <p> company: cnblogs </P> * @ Yang Guo under Bodhi * @ version 1.0 */import javax. naming. *; import Java. util. hashtable; public class jnditest {Static Context CTX = NULL; public static void test () {string key = "jmyang"; // bind BIND (key, "Yang junming") first "); // obtain the string value = (string) lookup (key); system. out. print (Key + "=" + value);}/** bind */public static void BIND (string name, String object) {hashtable ht = new hashtable (); ht. put (context. initial_context_factory, "weblogic. JNDI. wlinitialcontextfactory "); ht. put (context. provider_url, "T3: // localhost: 7001"); try {CTX = new initialcontext (HT); CTX. rebind (name, object);} catch (namingexception e) {e. printstacktrace ();} finally {try {CTX. close ();} catch (exception e) {e. printstacktrace () ;}}/ ***/public static object Lookup (string name) {hashtable ht = new hashtable (); ht. put (context. initial_context_factory, "weblogic. JNDI. wlinitialcontextfactory "); ht. put (context. provider_url, "T3: // localhost: 7001"); try {CTX = new initialcontext (HT); object = CTX. lookup (name); return object;} catch (exception e) {} finally {try {CTX. close ();} catch (exception e) {e. printstacktrace () ;}} return NULL ;}}

AboveCodeAfter execution, you may also view the JNDI tree on the Weblogic console for verification:

 

The JDBC data source is actually accessed using the JNDI service. The following is the JDBC sample code: (you must first create a data source in WebLogic)

Package jmyang. webLogic;/*** <p> title: JDBC example (WebLogic environment) </P> * <p> Description: </P> * <p> copyright: copyright (c) 2012 </P> * <p> company: cnblogs </P> * @ Yang Guo under Bodhi * @ version 1.0 */import Java. SQL. *; import javax. naming. *; import javax. SQL. *; import Java. util. hashtable; public class jdbctest {static final string weblogicserver = "T3: // localhost: 7001"; // Weblogic server address static final string weblogicindistr = "Weblogic. JNDI. wlinitialcontextfactory "; public static void test () {connection myconn = NULL; datasource DS = NULL; Context CTX = NULL; hashtable ht = new hashtable (); ht. put (context. initial_context_factory, weblogicindistr); ht. put (context. provider_url, weblogicserver); try {CTX = new initialcontext (HT); DS = (javax. SQL. datasource) CTX. lookup ("infoskysso"); // obtain the data source named infoskysso. Set Data Source in eblogic)} catch (namingexception e) {e. printstacktrace ();} statement mystatement = NULL; resstackset myresultset = NULL; try {myconn = Ds. getconnection (); // create a connection mystatement = myconn. createstatement (); myresultset = mystatement.exe cutequery ("select deptno from Dept where rownum <= 10"); While (myresultset. next () {system. out. println ("deptno =" + myresultset. getint ("deptno");} myresultset. c Lose ();} catch (sqlexception e) {system. out. println ("error code =" + E. geterrorcode (); system. out. println ("error message =" + E. getmessage ();} finally {// close the query if (mystatement! = NULL) {try {mystatement. close ();} catch (sqlexception e) {system. out. println ("error code =" + E. geterrorcode (); system. out. println ("error message =" + E. getmessage () ;}}// close the connection if (myconn! = NULL) {try {myconn. close ();} catch (sqlexception e) {system. out. println ("error code =" + E. geterrorcode (); system. out. println ("error message =" + E. getmessage ());}}}}}

Appendix: Method for creating a JDBC data source in WebLogic

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.