整理人:wangyl
注意
l 以下文档都是以mydomain为例
l 本系列文档都是使用相同的配置,即使用以前文档的配置,
1. 将附件中的classes12.zip拷贝到wlserver6.0\lib目录下
2. 修改config\mydomain\startWebLogic.cmd文件,在set CLASSPATH=。。。一行的最后加入如下语句
;.\lib\classes12.zip
3. 修改mydomain\config.xml文件,在最后加入如下语句
<JDBCConnectionPool
CapacityIncrement="1"
DriverName="oracle.jdbc.driver.OracleDriver"
InitialCapacity="2"
MaxCapacity="4"
Name="OracleThinPool"
Properties="user=zjcenter;password=zjcenter;dll=ocijdbc8;protocol=thin"
Targets="myserver"
URL="jdbc:oracle:thin:@172.16.91.36:1521:ORCL"//此处根据你的服务器地址做调整//
/>
<JDBCTxDataSource
JNDIName="WebApp.OracleThintxds"
Name="otxds"
PoolName="OracleThinPool"
Targets="myserver"
/>
4. 将附件中的sss.class拷贝到mydomain\applications\WebApp\WEB-INF\classes目录下
5. 修改mydomain\applications\WebApp\WEB-INF\web.xml文件,在
<web-app>
。。。。。。
。。。。。。
</web-app>
之间加入如下语句
<servlet>
<servlet-name>DataSourceTest</servlet-name>
<servlet-class>sss</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DataSourceTest</servlet-name>
<url-pattern>/dst/*</url-pattern>
</servlet-mapping>
6. 启动Weblogic Default Server
7. 访问我们的例子http://127.0.0.1:7001/WebApp/dst
8. 测试成功!
9. 例子sss.java
// Source File Name: sss.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletResponse;
import javax.servlet.http.*;
import javax.sql.DataSource;
public class sss extends HttpServlet
{
public sss()
{
}
public void service(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws IOException
{
httpservletresponse.setContentType("text/html");
PrintWriter printwriter = httpservletresponse.getWriter();
printwriter.println("<html><head><title>Hello datasource!</title></head><body>");
printwriter.println("<p><img src=images/BEA_Button_Final_web.gif align=right>");
printwriter.println("<H1>simplesqlServlet.java</h1> Attempting connection....");
Connection connection = null;
Statement statement = null;
try
{
InitialContext initialcontext = new InitialContext();
DataSource datasource = (DataSource)initialcontext.lookup("bbb.OracleThintxds");
connection = datasource.getConnection();
printwriter.println("<p>Connection successful...<p>Executing SQL...<p>");
statement = connection.createStatement();
try
{
statement.execute("drop table empdemo");
printwriter.println("Table empdemo dropped...<p>");
}
catch(SQLException sqlexception)
{
printwriter.println("<p>Table empdemo does not need to be dropped...<p>");
}
statement.execute("create table empdemo (empid int, name varchar(30), dept int)");
printwriter.println("<p>Table empdemo created...");
int i = statement.executeUpdate("insert into empdemo values (0,@#John Smith@#, 12)");
printwriter.println("<p>Number of rows inserted = " + i);
statement.execute("select * from empdemo");
ResultSet resultset = statement.getResultSet();
printwriter.println("<hr>Querying data ...<br>");
for(; resultset.next(); printwriter.println("<br><b>ID: </b> " + resultset.getString("empid") + "<br><b>Name:</b> " + resultset.getString("name") + "<br><b>Dept:</b> " + resultset.getString("dept") + "<hr>"));
ResultSetMetaData resultsetmetadata = resultset.getMetaData();
printwriter.println("Querying table meta data...<p>");
printwriter.println("Number of Columns: " + resultsetmetadata.getColumnCount() + "<b>");
for(int j = 1; j <= resultsetmetadata.getColumnCount(); j++)
{
printwriter.println("<p>Column Name: " + resultsetmetadata.getColumnName(j));
printwriter.println("<br>Nullable: " + resultsetmetadata.isNullable(j));
printwriter.println("<br>Precision: " + resultsetmetadata.getPrecision(j));
printwriter.println("<br>Scale: " + resultsetmetadata.getScale(j));
printwriter.println("<br>Size: " + resultsetmetadata.getColumnDisplaySize(j));
printwriter.println("<br>Column Type: " + resultsetmetadata.getColumnType(j));
printwriter.println("<br>Column Type Name: " + resultsetmetadata.getColumnTypeName(j));
printwriter.println("<br><hr>");
}
}
catch(Exception exception)
{
printwriter.println("Connection error:" + exception);
}
finally
{
try
{
if(connection != null)
connection.close();
printwriter.println("********************************");
printwriter.println("\n");
printwriter.println("stmt is");
printwriter.println(statement);
printwriter.println("\n");
printwriter.println("conn is");
printwriter.println(connection);
printwriter.println("********************************");
if(statement != null)
statement.close();
}
catch(SQLException sqlexception1)
{
printwriter.println("Exception during close()" + sqlexception1.getMessage());
}
}
printwriter.println("</b><h3>Example finished...</h3></body></html>");
}
}
文章来源于领测软件测试网 https://www.ltesting.net/