📄 dbtest.jsp
字号:
<%@ page language="java" import="java.sql.*,javax.naming.*,javax.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'dbtest.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Context ctx=null;
Connection cnn=null;
Statement stmt=null;
ResultSet rs=null;
try
{
ctx=new InitialContext();
if(ctx==null) throw new Exception("没有匹配的环境");
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/blog");
if(ds==null) throw new Exception("没有匹配数据库");
cnn=ds.getConnection();
stmt=cnn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql="select * from H_USER";
rs=stmt.executeQuery(sql);
out.println("数据库连接正常!");
}catch(Exception ee){
out.println("connect db error:"+ee.getMessage());
}
finally
{
if(rs!=null)rs.close();
if(stmt!=null)stmt.close();
if(cnn!=null)cnn.close();
if(ctx!=null)ctx.close();
}
%>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -