1导入包c3p0。
2.是mysql的Driver文件,mysql-connector-java-5.1.6-bin.ja要COPY到 Tomcat_Home/common/lib/ 目录下
3.Tomcat_Home/conf 中的 server.xml 文件
在 <GlobalNamingResources> 标签中添加代码,就是下面有颜色的那部分~~~~,并且将相应参数修改你项目里的配置....
<!-- Global JNDI resources -->
<GlobalNamingResources>
<Resource
name="jdbc/MessageBoard"
type="javax.sql.DataSource"
password="root"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="5"
maxWait="5000"
username="root"
url="jdbc:mysql://localhost/learnjsp"
maxActive="15"
/></GlobalNamingResources>
4.Tomcat_Home/conf 中的 context.xml 文件,填上如下代码....
<!-- The contents of this file will be loaded for each web application -->
<Context>
<ResourceLink global="jdbc/MessageBoard" name="jdbc/MessageBoard" type="javax.sql.DataSource"/>
</Context>
5使用
Connection conn=null;
try {
Context context=new InitialContext();
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/MessageBoard");
conn=ds.getConnection();
} catch (NamingException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
Statement stmt=null;
ResultSet rs=null;
try {
stmt=conn.createStatement();
String sql="select item_id from bbsitems where fathers_id='"+id+"' and item_id!='"+id+"'";
rs=stmt.executeQuery(sql);
if(rs.next())