锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
浜?鏁版嵁搴撻┍鍔ㄩ厤緗?br />copy mysql-connector-java-3.1.11-bin.jar to %WL_HOME%\
鍦╠:\SoftWare\BEA\user_projects\domains\datasource\startWebLogic.cmd鐨剆et CLASSPATH=閮ㄥ垎鍔犲叆%WL_HOME%\mysql-connector-java-3.1.11-bin.jar
涓?閰嶇疆鏁版嵁搴撹繛鎺ユ睜
1.start menu->program->BEA Weblogic Platfrom 8.1->User Projects->datasource->Start Server
2.鎵撳紑http://127.0.0.1:7001/console weblogic 12345678
3.datasource->鏈嶅姟->JDBC->榪炴帴緙撳啿姹?Connections Pools)->(閰嶇疆鏂扮殑JDBC榪炴帴緙撳啿姹?configure a new JDBC Connection Pools
4.Database Type 閫夋嫨mysql椹卞姩涓簅rg.gjt.mm.mysql.Driver
5.Continue
鍚嶇О:MyJDBC Connection Pool
鏁版嵁搴撳悕縐?datasource
涓繪満鍚?127.0.0.1
绔彛:3306
鏁版嵁搴撶敤鎴峰悕:
瀵嗙爜:
紜瀵嗙爜:
5.Continue->Test Driver Configuration
6.Create and deploy
鍥?閰嶇疆鏁版嵁婧?br />1.datasource->鏈嶅姟->JDBC->鏁版嵁婧?Data Source)->configure a new JDBC Data Source
2.
鍚嶇О:MyData Source
JNDI鍚嶇О:MyData Source
3.Continue->Continue->Create
------------嫻嬭瘯浠g爜DSTest.java------------------------
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
public class DSTest{
聽private static Context getInitialContext(){
聽聽String URL="t3://127.0.0.1:7001";
聽聽String user="weblogic";
聽聽String password="12345678";
聽聽Properties prop=null;
聽聽Context context=null;
聽聽
聽聽try{
聽聽聽prop = new Properties();
聽聽聽prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
聽聽聽prop.put(Context.PROVIDER_URL,URL);
聽聽聽prop.put(Context.SECURITY_PRINCIPAL,user);
聽聽聽prop.put(Context.SECURITY_CREDENTIALS,password);
聽聽聽context = new InitialContext(prop);
聽聽}
聽聽catch(Exception e)
聽聽{
聽聽聽e.printStackTrace();
聽聽}
聽聽return context;
聽聽}
聽
聽public static void main(String[] args){
聽聽DSTest dstest = new DSTest();
聽聽
聽聽DataSource ds=null;
聽聽Context ctx=null;
聽聽Connection conn=null;
聽聽
聽聽try{
聽聽聽ctx = getInitialContext();
聽聽聽ds = (DataSource)ctx.lookup("MyData Source");
聽聽}
聽聽catch(Exception e){
聽聽聽e.printStackTrace();
聽聽}
聽聽
聽聽Statement stmt=null;
聽聽ResultSet rs=null;
聽聽
聽聽try{
聽聽聽conn = ds.getConnection();
聽聽聽stmt = conn.createStatement();
聽聽聽String sql = "select * from user";
聽聽聽rs = stmt.executeQuery(sql);
聽聽聽
聽聽聽while(rs.next()){
聽聽聽聽System.out.println("username : " + rs.getString("username"));
聽聽聽聽System.out.println("password : " + rs.getString("password"));
聽聽聽聽}
聽聽}
聽聽catch(SQLException e){
聽聽聽e.printStackTrace();
聽聽聽}
聽聽finally{
聽聽聽try{
聽聽聽if(stmt != null) { stmt.close();}
聽聽聽if (conn != null) { conn.close();}
聽聽聽}
聽聽聽catch(SQLException e){
聽聽聽聽e.printStackTrace();
聽聽聽聽}
聽聽}
聽聽}
}
--------------------------------------------------------------------------------------------
set CLASSPATH=%CLASSPATH%;weblogic.jar
javac DSTest.java
java DSTest
pause
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
<%
DataSource ds=null;
Connection conn=null;
Statement stmt=null;
try{
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/MyDataSource");
conn = ds.getConnection();
stmt = conn.createStatement();
String strSql="select * from user";
ResultSet rs=stmt.executeQuery(strSql);
while(rs.next()){
聽out.println("鐢ㄦ埛鍚嶄負 : " + rs.getString(1));
聽}
聽if(rs!=null) {rs.close();};
聽if(stmt!=null) {stmt.close();};
聽if(conn!=null) {conn.close();};
}
catch (SQLException e) {
聽聽聽聽 // display SQL specific exception information
聽聽聽聽 System.out.println("*************************" );
聽聽聽聽 System.out.println("SQLException in main: " + e.getMessage() );
聽聽聽聽 System.out.println("** SQLState: " + e.getSQLState());
聽聽聽聽 System.out.println("** SQL Error Code: " + e.getErrorCode());
聽聽聽聽 System.out.println("*************************" );
聽聽聽聽 e.printStackTrace();
}
catch (Exception e) {
聽聽聽聽 System.out.println("Exception in main: " + e.getMessage() );
聽聽聽聽 e.printStackTrace();
}
%>
</head>
<body>
</body>
</html>
聽