JNDIを介してサーブレットからJDBCへのオブジェクトを検索したいと思います。
このプロセスの正確なコードは何ですか?
2 に答える
3
サーブレットコンテナによって異なる場合があります。
String initialContext = "java:comp/env";
Context env = (Context) new InitialContext().lookup(initialContext);
Object o = env.lookup(name);
于 2009-06-05T07:44:21.857 に答える
-1
このコードを try ブロックで使用します
Context initContext = new InitialContext();
Context envContext = (Context)
initContext.lookup("java:comp/env");
DataSource ds = (DataSource)
envContext.lookup("jdbc/UsersDB");
Connection connection = ds.getConnection();
これをコンテキストに追加します。xml
<Resource
name="jdbc/UsersDB"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://192.168.1.56:1433;DatabaseName=testdb1;"
username="uname"
password="pwd"
/>
web.xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/UsersDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
于 2018-03-20T14:13:08.480 に答える