一.配置weblogic服务器
start menu->program->BEA Weblogic Platfrom 8.1->Configuration Wizard->Create a new Weblogic->next->next->next->(user password)next->(select jdk)next->(datasource)create
二.数据库驱动配置
copy mysql-connector-java-3.1.11-bin.jar to %WL_HOME%\
在d:\SoftWare\BEA\user_projects\domains\datasource\startWebLogic.cmd的set 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驱动为org.gjt.mm.mysql.Driver
5.Continue
名称:MyJDBC Connection Pool
数据库名称:datasource
主机名:127.0.0.1
端口:3306
数据库用户名:
密码:
确认密码:
5.Continue->Test Driver Configuration
6.Create and deploy
四.配置数据源
1.datasource->服务->JDBC->数据源(Data Source)->configure a new JDBC Data Source
2.
名称:MyData Source
JNDI名称:MyData Source
3.Continue->Continue->Create
------------测试代码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