0, launching ClassPath in WebLogic Adds MySQL's JDBC library file. 1. Create a connection pools in WL, you can test 2, then create a DataSource, and the pool name points to the connection pools just configured, specifying this DataSource's JNDI name as "MySQLPool" 3, establish the client test program package COM. CATMIW.TESTMYSQL;
Import javax.naming. *; import javax.sql.datasource; import java.sql. *; import java.util.properties;
public class MysqlPool {private static Context getInitialContext () throws Exception {String url = "t3: // localhost: 7001"; String user = null; String password = null; Properties properties = null; try {properties = new Properties (); properties.put (Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); properties.put (Context.PROVIDER_URL, url); if (user = null!) {properties.put (Context.SECURITY_PRINCIPAL, user); properties.put (Context.security_credentials, password == null? "": Password);
return new InitialContext (properties);} catch (Exception e) {System.out.println ( "Unable to connect to WebLogic server at" url); System.out.println ( "Please make sure that the server is running." Throw e;}}}}
Public static void main (string args []) throws exception {context (); // Get initialization context Object Obj = ctx.lookup ("mysqlpool"); // Find connection pool Datasource DS = (DataSource) OBJ; // Convert to DataSource
Try {connection connect = ds.getConnection (); // Get a connection in the connection pool ("Success Connect MySQL Connection Pool!");
Statement Stmt = Connect.createStatement (); ResultSet RS = Stmt.executeQuery ("SELECT * FROT"); while (rs.next ()) {system.out.println (rs.getstring ("name"));} } Catch (exception e) {system.out.print ("get Data Error!"); E.PrintStackTrace ();}
}} Run results: Success Connect MySQL Connection Pool!