刚刚开始学 测试数据源的小程序 怎么也搞不好
相关文件如下
==============================================================================================================
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class Dstry { public static void main(String[] args) {
String sql = "insert into users(username, password) values(?,?)";
try {
Context context = new InitialContext();
DataSource ds=(DataSource)context.lookup("java:com/env/jdbc/oracleds");
Connection conn=ds.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "yijianlian");
pstmt.setString(2, "123");
pstmt.setString(1, "alston");
pstmt.setString(2, "12");
pstmt.executeUpdate(sql);
pstmt.close();
conn.close(); } catch (NamingException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
}}
====================================================================
context .xml
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context reloadable="true"> <!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource> <Resource
name="jdbc/oracleds"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
maxIdle="2"
maxWait="5000"
username="scott"
password="tiger"
url="jdbc:oracle:thin:@192.168.4.100:1521:ora9"
maxActive="100"/>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
--> <!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
--></Context>
报错如下
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at Dstry.main(Dstry.java:17)
相关文件如下
==============================================================================================================
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class Dstry { public static void main(String[] args) {
String sql = "insert into users(username, password) values(?,?)";
try {
Context context = new InitialContext();
DataSource ds=(DataSource)context.lookup("java:com/env/jdbc/oracleds");
Connection conn=ds.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "yijianlian");
pstmt.setString(2, "123");
pstmt.setString(1, "alston");
pstmt.setString(2, "12");
pstmt.executeUpdate(sql);
pstmt.close();
conn.close(); } catch (NamingException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
}}
====================================================================
context .xml
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context reloadable="true"> <!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource> <Resource
name="jdbc/oracleds"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
maxIdle="2"
maxWait="5000"
username="scott"
password="tiger"
url="jdbc:oracle:thin:@192.168.4.100:1521:ora9"
maxActive="100"/>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
--> <!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
--></Context>
报错如下
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at Dstry.main(Dstry.java:17)
这个是固定格式
java:comp/env/jdbc/oracleds
debug下
<resource-ref>
<description>
Resource reference to a factory for java.sql.Connection
instances that may be used for talking to a particular
database that is configured in the server.xml file.
</description>
<res-ref-name>
jdbc/oracleds
</res-ref-name>
<res-type>
javax.sql.DataSource
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>