Tomcat7.0连接mysql5.0
1,mysql-connector-java-5.0.4-bin.jar 放入到 C:/Program Files/Apache Software Foundation/Tomcat 7.0/lib
2,项目的web.xml与tomcat一致
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>webt3</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <resource-ref> <description>Tomcat 7 DBCP</description> <res-ref-name>jdbc/mysql5</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref></web-app>3,tomcat/conj中的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> <!-- 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" /> --> <Resource name="jdbc/mysql5" auth="Container" type="javax.sql.DataSource" maxActive="80" maxIdle="20" maxWait="10000" removeAbandoned="true" username="root" password="5201314" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/webt3" /></Context>
4,测试:index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ page import="java.sql.*, javax.sql.*, javax.naming.*" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><%Connection conn=null;PreparedStatement stmt=null;ResultSet rs=null;String sql=null;String username=null;String alias=null;String pwd=null;String strMsg=null;try{ Context cxt = new InitialContext(); DataSource ds = (DataSource)cxt.lookup("java:/comp/env/jdbc/mysql5"); conn = ds.getConnection(); sql="SELECT 1 "; stmt=conn.prepareStatement(sql); rs=stmt.executeQuery(sql); if( rs.next() ){ strMsg="连接ok"; out.println("连接ok"); }else{ out.println("rs.next() fail"); } rs.close(); } catch(Exception e){ out.println("连接失败:"+e.getMessage()); //e.printStackTrace();}try{ if(stmt!=null) stmt.close();}catch(Exception e){}try{ if(conn!=null) conn.close();}catch(Exception e){}%></body></html>OK,
则成功
>更多相关文章
- 10-26高手浅谈MySQL数据库的几个安全问题
- 10-26MySQL False 黑客注入及技巧总结
- 02-2514种最好方法保护MySQL全面安全
- 12-23mysqltoolkit用法[备忘]
- 12-23一个基于MySQL的Key-List存储方案
- 12-21ODBC中遇到的错误
- 12-21使用mysql遇到的问题
- 12-21PAIP.MYSQLSLEEP连接太多解决
首页推荐
佛山市东联科技有限公司一直秉承“一切以用户价值为依归
- 01-11全球最受赞誉公司揭晓:苹果连续九年第一
- 12-09罗伯特·莫里斯:让黑客真正变黑
- 12-09谁闯入了中国网络?揭秘美国绝密黑客小组TA
- 12-09警示:iOS6 惊现“闪退”BUG
- 12-25优酷推出U镜到底等直播功能 已应用在羽毛球
- 12-25百川智能正式发布全链路领域增强大模型
- 12-25SHEIN4家仓储物流园获“零废工厂”认证
- 12-25西方博主在TikTok上展现中国风貌,“China
- 12-05亚马逊推出新一代基础模型 任意模态生成大模
相关文章
24小时热门资讯
24小时回复排行
热门推荐
最新资讯
操作系统
黑客防御