我的记录是随机的,遇见啥就记录啥。
今天学习Tomcat JSDI数据源技术只遇见了一个问题 就是报错:
Name jdbc is not bound in this Context :not find [jdbc]
原因是在教学书上写时是建议在WEB工程目录下建立文件context.xml,并配置数据源信息。但是,按照书上的使用的话,就会报这个错。其实这个错误也很明显的提醒你是什么问题了,是找不到jdbc这个数据源,就可能是ctx.lookup("jdbc")的时候根本就找不到。经过网上找寻资料后,我最终还是选择了在Tomcat的目录下的conf/context.xml配置数据源信息,结果就成功了
。可能是我水平不到家的问题,不会使用在WEB工程目录下建立的文件context.xml .
.接下来附上.java
.xml.的代码.
.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 path="/MyServer" reloadable="true" crossContext="true">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- 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" />
-->
<Loader delegate="true" />
<Resource name="JDBC"
type="javax.sql.DataSource"
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/data"
username="root"
password="123"
maxActive="4"
maxIdle="2"
maxWait="6000" />
</Context>
WEB工程目录下的,在<web-app>内
web.xml
<resource-ref>
<description>DB</description>
<res-ref-name>JDBC</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
.java
public void getNewuser(String name){ //Tomcat JSDI数据源技术
Connection conn = null;
PreparedStatement pstmt = null;
DataSource ds= null;
String sql = "insert into user(ID,password,name,mark)value(?,?,?,?)";
String [] strs = name.split("[,]");
System.out.print(name);
System.out.print(strs[0]+strs[1]);
System.out.print(strs[2]);
try {
InitialContext ctx=new InitialContext(); //暂时没找出context.xml在WENT-INF下的使用方法
ds = (DataSource)ctx.lookup("java:comp/env/JDBC"); //暂时使用context.xml在Tomcat的conf下的使用方法
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, Integer.valueOf(strs[0]));
pstmt.setInt(2,Integer.valueOf(strs[1]));
pstmt.setString(3,strs[2]);
pstmt.setInt(4,0);
pstmt.addBatch(); //批执行
int[] row = pstmt.executeBatch(); //批执行
if(row.length>0){
System.out.print("成功增了"+row.length+"条记录");
}
/* Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/data";
String username ="root";
String password = "123";
Connection conn = DriverManager.getConnection(url,username,password);
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,Integer.valueOf(strs[0]));
ps.setInt(2,Integer.valueOf(strs[1]));
ps.setString(3,strs[2]);
ps.setInt(4,0);
ps.addBatch();
int[] row = ps.executeBatch();
if(row.length>0) {
System.out.print("成功增了"+row.length+"条记录");
}*/
pstmt.close();
conn.close();
} catch(Exception e){
e.printStackTrace();
System.out.print("错误");
}
// return retList.toArray(new user[0])
}