1个回答
2016-06-29 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
在jsp中获得数据源的方法是通过jdbc或者datasource连接到数据库然后获取得到。
1、jsp页面代码:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:useBean id="db" class="cc.openhome.DatabaseBean"/>
<!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>测试数据库连接</title>
</head>
<body>
<c:choose>
<c:when test="${db.connectedOK}">连接成功!</c:when>
<c:otherwise>连接失败!</c:otherwise>
</c:choose>
</body>
</html>
2、在tomcat中配置
JDBCDemo context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/JDBCDemo">
<Resource name="jdbc/demo"
auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="root"
password="123456" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/demo?
useUnicode=true&characterEncoding=UTF8"/>
</Context>
3、在java后台实现数据源的连接
import java.io.Serializable;import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
public class DatabaseBean implements Serializable {
private DataSource dataSource;
public DatabaseBean() {
try {
Context initContext = new InitialContext();
Context envContext = (Context)
initContext.lookup("java:/comp/env");
dataSource = (DataSource) envContext.lookup("jdbc/demo");
} catch (NamingException ex) {
throw new RuntimeException(ex);
}
}
public boolean isConnectedOK() {
boolean ok = false;
Connection conn = null;
SQLException ex = null;
try {
conn = dataSource.getConnection();
if (!conn.isClosed()) {
ok = true;
}
} catch (SQLException e) {
ex = e;
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
if(ex == null) {
ex = e;
}
}
}
if(ex != null) {
throw new RuntimeException(ex);
}
}
return ok;
}
}
1、jsp页面代码:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:useBean id="db" class="cc.openhome.DatabaseBean"/>
<!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>测试数据库连接</title>
</head>
<body>
<c:choose>
<c:when test="${db.connectedOK}">连接成功!</c:when>
<c:otherwise>连接失败!</c:otherwise>
</c:choose>
</body>
</html>
2、在tomcat中配置
JDBCDemo context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/JDBCDemo">
<Resource name="jdbc/demo"
auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="root"
password="123456" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/demo?
useUnicode=true&characterEncoding=UTF8"/>
</Context>
3、在java后台实现数据源的连接
import java.io.Serializable;import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
public class DatabaseBean implements Serializable {
private DataSource dataSource;
public DatabaseBean() {
try {
Context initContext = new InitialContext();
Context envContext = (Context)
initContext.lookup("java:/comp/env");
dataSource = (DataSource) envContext.lookup("jdbc/demo");
} catch (NamingException ex) {
throw new RuntimeException(ex);
}
}
public boolean isConnectedOK() {
boolean ok = false;
Connection conn = null;
SQLException ex = null;
try {
conn = dataSource.getConnection();
if (!conn.isClosed()) {
ok = true;
}
} catch (SQLException e) {
ex = e;
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
if(ex == null) {
ex = e;
}
}
}
if(ex != null) {
throw new RuntimeException(ex);
}
}
return ok;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |