日期:2014-05-16  浏览次数:20485 次

链接数据库和链接数据库池
datalink.jsp

<%@ page contentType="text/html;charset=gb2312"%>
  <%@ page import="java.sql.*"%>
  <html>
  <body>
  <%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
  String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=studenttest";
  String user="sa";
  String password="sa";  
  Connection conn= DriverManager.getConnection(url,user,password);
  Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  String sql="select * from student";
  ResultSet rs=stmt.executeQuery(sql);
  while(rs.next()) {%>
  您的第一个字段内容为:<%=rs.getString(1)%> <br>
  您的第二个字段内容为:<%=rs.getString(2)%> <br>
  您的第三个字段内容为:<%=rs.getString(3)%> <br>
  <%}%>
  <%out.print("数据库操作成功,恭喜你  ");%>
  <%rs.close();
  stmt.close();
  conn.close();  
  %>
  <a href="index.htm">返回首页</a> 



dataPoolLink.jsp

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="javax.naming.Context"%>
<%@ page import="javax.sql.DataSource"%>
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数据库连接池试验</title>
</head>
<body>
<%
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/myjsp2");
Connection conn = ds.getConnection();
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from class";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%> <br>
您的第二个字段内容为:<%=rs.getString(2)%> <br>

<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();  
%>