jdbc在myeclipse中与SQL SERVER 2008显示连接成功,但是在网页中找不到数据库里
网页代码如下:
login.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!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>
<%
String err=(String)session.getAttribute("err");
if(err!=null&&!err.equals("")){
out.print("<font color=red>*"+err+"</font>");
}
%>
<form action="loginapp.jsp" method="POST">
用户ID:<input type="text" name="id" /><br>
密码:<input type="password" name="pwd" /><br>
<input type="submit" value="登录" />
</form>
</body>
</html>
loginapp.jsp
<%@ page language="java" import="java.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 'loginapp.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">
</head>
<body>
<%
String err="";
String id,pwd;
id=request.getParameter("id");
pwd=request.getParameter("pwd");
String toUrl="login.jsp";
if(id!=null){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;databasename=student";
Connection conn=DriverManager.getConnection(url,"sa","z19840814");
Statement stat=conn.createStatement();
ResultSet rs=stat.executeQuery("select * from student where id='"+id+"' and pwd='"+pwd+"'");
if(rs.next()){
session.setAttribute("id",id);
toUrl="showinfo.jsp";
}else{
err="用户名或密码错误,请重新输入";
}
conn.close();
}catch(Exception ex){
err="err:"+ex.toString();
}
}
session.setAttribute("err",err);
response.sendRedirect(toUrl);
%>
</body>
</html>
showinfo.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
String id=(String)session.getAttribute("id");
if(id==null){
response.sendRedirect("login.jsp");
}else{
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'showinfo.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">
</head>
<body>
<%
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;databasename=student";
Connection conn=DriverManager.getConnection(url,"sa","z19840814");
Statement stat=conn.createStatement();
ResultSet rs=stat.executeQuery("select * from student where id='"+id+"'");
if(rs.next()){
String sname=rs.getString("sname");
String sgrade=rs.getString("sgrade");
Date sbirth=rs.getDate("sbirth");
%>
学号:<%=id%><br>
姓名:<%=sname%><br>
班级:<%=sgrade%><br>
出生日期:<%=sbirth%><br>
<%
}
conn.close();
}catch(Exception ex){
out.println(ex.toString());
}
%>
</body>
</html>
<%
}
%>