server.xml 配置:
<host>
...
<Context path="/test" docBase="test"
debug="5" reloadable="true" crossContext="true">
<Resource
name="jdbc/conMysql"
type="javax.sql.DataSource"
password="mypass"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://localhost/mysql"
maxActive="8"/>
</Context>
</host>
---------------------------------------------------------------------------
webapps\test\WEB-INF\web.xml 配置:
<web-app>
...
<resource-ref>
<description>Tomcat Datasource</description>
<res-ref-name>jdbc/conMysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
------------------------------------------------------------------------------
jsp 代码:
<%@ page language="java" %>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*,javax.naming.Context,javax.naming.InitialContext"%>
<%@ page import="java.sql.*,javax.sql.DataSource"%>
<html>
<head>
<title>JSP连接MYSQL数据池</title>
<head>
<body>
<% //声名
Statement stmt = null;
ResultSet rs = null;
try
{
// Obtain our environment naming context
Context initCtx = new InitialContext();
if ( initCtx == null ) {
throw new Exception("Uh oh -- no context!");
} Context envCtx = (Context) initCtx.lookup("java:comp/env"); // Look up our data source
DataSource ds = (DataSource) envCtx.lookup("jdbc/conMysql");
if (ds == null)
{
throw new Exception("Data source not found!");
}
else
{
Connection conn = ds.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * From czy");
while (rs.next())
{
%>
<%=rs.getString("czyid")%>
<%=rs.getString("name")%>
<%=rs.getString("pwd")%><br/> <%
} }
rs.close(); //关闭ResultSet对象
stmt.close(); //关闭Statement对象
conn.close(); //关闭Connection对象 }
catch (Exception e){
out.println(e);
}
%>
</body>
</html>
<host>
...
<Context path="/test" docBase="test"
debug="5" reloadable="true" crossContext="true">
<Resource
name="jdbc/conMysql"
type="javax.sql.DataSource"
password="mypass"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://localhost/mysql"
maxActive="8"/>
</Context>
</host>
---------------------------------------------------------------------------
webapps\test\WEB-INF\web.xml 配置:
<web-app>
...
<resource-ref>
<description>Tomcat Datasource</description>
<res-ref-name>jdbc/conMysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
------------------------------------------------------------------------------
jsp 代码:
<%@ page language="java" %>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*,javax.naming.Context,javax.naming.InitialContext"%>
<%@ page import="java.sql.*,javax.sql.DataSource"%>
<html>
<head>
<title>JSP连接MYSQL数据池</title>
<head>
<body>
<% //声名
Statement stmt = null;
ResultSet rs = null;
try
{
// Obtain our environment naming context
Context initCtx = new InitialContext();
if ( initCtx == null ) {
throw new Exception("Uh oh -- no context!");
} Context envCtx = (Context) initCtx.lookup("java:comp/env"); // Look up our data source
DataSource ds = (DataSource) envCtx.lookup("jdbc/conMysql");
if (ds == null)
{
throw new Exception("Data source not found!");
}
else
{
Connection conn = ds.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * From czy");
while (rs.next())
{
%>
<%=rs.getString("czyid")%>
<%=rs.getString("name")%>
<%=rs.getString("pwd")%><br/> <%
} }
rs.close(); //关闭ResultSet对象
stmt.close(); //关闭Statement对象
conn.close(); //关闭Connection对象 }
catch (Exception e){
out.println(e);
}
%>
</body>
</html>
解决方案 »
- 获取HTML源码文档中<div class="stat stat_area"></div>格式中的内容
- 读取xml文件获取String字符串为乱码!怎么解决?
- httpClient 如何获取页面传来特定的字符串????
- 大伙给帮忙看看
- struts2的action中validate()验证的难题,在线,希望有人能解决!
- 没学过jsp,想将asp转化为jsp
- 这是错在哪里?
- java讨论群4549203
- 在同一个页面里为什么不可以两次用同一个字段的值???
- 请问在JSP中怎么得到有多少条记录,象asp中的rs.recordcount()那样?
- 关于论坛贴子置顶!!!!!!!!!!
- 急,关于套接字编程问题,各位高手请进
Generated servlet error:
conn cannot be resolved
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
<%@ page language="java" %>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*,javax.sql.*"%>
<html>
<head>
<title>JSP连接MYSQL数据库</title>
<head>
<body>
<%
//声名
String driver="com.mysql.jdbc.Driver";
String sConnStr ="jdbc:mysql://localhost/mysql";
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(sConnStr,"root","mypass");
Statement stmt = null;
ResultSet rs = null;
String sql="select pwd from czy where czyID = '001' ";stmt = conn.createStatement();
rs = stmt.executeQuery(sql);//利用while循环将数据表中的记录列出
while (rs.next())
{
%>
<%=rs.getString("pwd")%><br/>
<%
}
rs.close(); //关闭ResultSet对象
stmt.close(); //关闭Statement对象
conn.close(); //关闭Connection对象
%>