login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>Login.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
============== 欢迎登陆学生管理系统 ================
<br>
<br>
<form name=" form1" method="post" action="Login_check.jsp">
<br>
用户:š
<input type="text" name="username">
<br>
<br>
密码:š
<input type="password" name="password" id="p">
<br>
<br>
<input name="Submit" type="submit" id=" Submit" value="提交">
<input name="Submit2" type="reset" value="重置">
<a href="Regester.jsp">用户注册</a>
</form>
</body>
</html>
logincheck.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page language="java" import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%
request.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Login_check.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>
<%
String name = request.getParameter("username");
//String str=(String)request.getAttribute("name");
String pwd = request.getParameter("password");
//System.out.println(name +" "+str);
System.out.println(name + " ");
System.out.println(pwd + " ");
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.print("驱动包连接失败!");
}
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=StuInfor";
String user = "sa";
String password = "sa";
Connection con = null;
PreparedStatement psta = null;
ResultSet rs = null;
String sql = "select * from Student where username='" + name
+ "' and password='" + pwd + "'";
try {
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
}
try {
psta = con.prepareStatement(sql);
rs = psta.executeQuery();
if (rs.next()) {
//session.setAttribute("Login_name", name); //成功登陆的用户存储到session对象中
response.sendRedirect("Main.jsp"); //登陆成功后转向到mian.jsp页面中
} else {
//response.("index.jsp"); //登陆失败转向到index.jsp页面中
response.sendRedirect("index.jsp"); //登陆失败转向到index.jsp页面中
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
psta.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//}
%>
</body>
</html>
用户注册
regester.jsp
<%@ page language="java" import="java.util.*" 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 'index.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>
============== 欢迎来到注册主界面 ===============
<form name="form1" action="Regester_check.jsp" method="post">
用户名:
<input type="text" name="username">
<br>
<br>
密码:
<input type="password" name="password">
<br>
性别:
<input name="Sex" type="radio" value="1" />
男
<input name="Sex" type="radio" value="2" />
女
<br>
email:
<input type="text" name="emailname">
<br>
<br>
电话:
<input type="text" name="mobilename">
<br>
<br>
<input name="Submit" type="submit" id=" Submit" value="提交">
<input name="Submit2" type="reset" value="重置">
</form>
</body>
</html>
regestercheck.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page language="java" import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%
request.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Regester_check.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>
<%
String Username = request.getParameter("username");
String Pwd = request.getParameter("password");
String Sex = request.getParameter("Sex");
String Email = request.getParameter("emailname");
String Mobile = request.getParameter("mobilename");
out.print(Username); //测试一下看有没有获取过来
out.print(Pwd);
//1.加载驱动
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.print("驱动包连接失败!");
}
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=StuInfor";
String user = "sa";
String password = "sa";
Connection con = null;
PreparedStatement psta = null;
ResultSet rs = null;
//String sql = "INSERT INTO Student VALUES (7,'三',23,'23@',1223,555)";
//String sql = "INSERT INTO Student VALUES (?,?)";
String sql1 = "select * from Student where username='" +Username
+ "' and password='" + Pwd + "'";
String sql2 = "INSERT INTO [Student] VALUES(?,?,?,?,?)";
//2.连接数据库
try {
con = DriverManager.getConnection(url, user, password);
//3.创建 PreparedStatement对象
psta = con.prepareStatement(sql1);
rs = psta.executeQuery();
if (rs.next()) { //判定条件是:如果以上sql语句能执行的话,侧说明用户已经存在了, 所以提交给一个用户已存在页面
response.sendRedirect("Rfail.jsp");
} else {
psta = con.prepareStatement(sql2);
// 创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。
psta.setString(1, Username);
psta.setString(2, Pwd);
psta.setString(3, Sex);
psta.setString(4, Email);
psta.setString(5, Mobile);
int m = psta.executeUpdate();
if (m > 0) {
response.sendRedirect("Rsucess.jsp"); //跳转到成功页面
} else {
response.sendRedirect("Rfail.jsp"); //跳转到失败页面
}
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
}
//5.释放资源
finally {
try {
if (rs != null) {
rs.close();
}
if (psta != null) {
psta.close();
}
if (con != null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
%>
</body>
</html>
用户修改
view_news.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%@ page language="java" import="com.suhu.www.News" %>
<%request.setCharacterEncoding("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 'view_news.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>
<script language="javascript">
function login(){
documnet.longin.submit();
}
</script>
<p align="center">你好欢迎来到新闻资讯频道</p>
<form name="form1" method="post" action="" >
<table width="570" border="1" align="center" cellpadding="0" class="word_deepgrey">
<tr>
<td align="center">
<select name="select" id="select">
<option value="1" selected="selected">标题</option>
<option value="2" selected="selected">日期</option>
<option value="1" selected="selected">类型</option>
</select><span>查询信息</span> <input type="text" name="sa" id="sa" size="20" > <input type="submit" name="Submit" value="查询">
</tr>
</table>
</form>
<jsp:useBean id="da" scope="page" class="com.suhu.www.News" ></jsp:useBean>
<%
String s1="select * from mynews";
ResultSet rs =da.executeQuery(s1);
while(rs.next()){ %>
<table width="570" height="35" border="2" align="center">
<tr>
<td align="center" height="40" ><%=rs.getString("news_id")%></td>
<td align="center" height="40" width="100"><%=rs.getString("news_date").substring(0,10)%></td>
<td align="center" height="40" width="100"><%=rs.getString("news_num")%></td>
<td align="center" height="40" width="100"><%=rs.getString("news_type")%></td>
<td align="center" height="40" width="100"><%=rs.getString("news_title")%></td>
<td align="center" height="40" width="100"><a href='infor.jsp?id=<%=rs.getInt(1)%>'> <%=rs.getString("news_text").substring(0,2)%>" </a></td>
</tr>
</table>
<%
}
%>
<%
da.close();
%>
</body>
</html>
info.jsp
<%@ page language="java" import="java.util.*,java.sql.*"
pageEncoding="UTF-8"%>
<%@ page import="com.suhu.www.News"%>
<%request.setCharacterEncoding("UTF-8"); %>
<%
String Id = request.getParameter("id");
%>
<%
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 'infor.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>
<jsp:useBean id="db" class="com.suhu.www.News" scope="page"></jsp:useBean>
<form action="change.jsp?news_id=<%=Id%>" method="post" name="form1">
<%
//out.print(Id);
String s = "select * from mynews where news_id=" + Id;
ResultSet rs = db.executeQuery(s);
if (rs.next()) {
String str = rs.getString(6);
%>
<p align="center">
<label>
<textarea name="textare" cols="75" rows="24">
<%=str%>
</textarea>
</label>
<br>
</p>
<%
}
%>
<%
db.close();
%>
<table width="570" border="1" align="center" cellpadding="0"
class="word_deepgrey">
<tr>
<td align="center">
<input type="submit" value="保存修改" name="Submit">
</td>
<td align="center">
<a href="view_news.jsp">返回主页</a>
</td>
</tr>
</table>
</form>
</body>
</html>
change.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%@ page language="java" import="com.suhu.www.News" %>
<%request.setCharacterEncoding("UTF-8"); %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<% String Id=request.getParameter("news_id");
String NewsText=request.getParameter("textare");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'change.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>
<jsp:useBean id="db" class="com.suhu.www.News" scope="page"></jsp:useBean>
<% String str="update mynews set news_text='"+NewsText+"'where news_id='"+Id+"'";
int i=db.excuteUpdate(str);
out.print(i);
if(i!=0){
response.sendRedirect("view_news.jsp");
}
else{
response.sendRedirect("failure.jsp");
}
%>
<body>
</body>
</html>
fail.jsp
<%@ page language="java" import="java.util.*" 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 'failure.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>
<h3 align="center">对不起你修改失败</h3>
<table width="570" border="0" align="center" cellpadding="0" class="word_deepgrey">
<td align="center"> <a href="view_news.jsp">返回从新修改</a></td>
</table>
</body>
</html>
javabean
package com.suhu.www;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class News {
private String url = "jdbc:sqlserver://localhost:1433;DatabaseName=StuInfor";;
private String user = "sa";
private String password = "sa";
private Connection con = null;
private PreparedStatement psta = null;
// private String sql="select * from mynews where news_id=2";
private ResultSet rs = null;
public News() { // 构造方法对其初始化
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.print("驱动包连接失败!");
}
}
public ResultSet executeQuery(String s) {
rs = null;
try {
con = DriverManager.getConnection(url, user, password);
psta = con.prepareStatement(s, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// 创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。
rs = psta.executeQuery();
// 在此 PreparedStatement 对象中执行 SQL 查询,并返回该查询生成的 ResultSet 对象
// 4.处理结果集,并调出数据库中的信息打印在控制台上
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("数据库连接失败!");
}
return rs;
}
public int excuteUpdate(String s) {
int i = 0;
try {
con = DriverManager.getConnection(url, user, password);
psta = con.prepareStatement(s);
i = psta.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
public void close() {
try {
if (rs != null) {
rs.close();
}
if (psta!= null){
psta.close();
}
if (con!= null){
con.close();}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
}
}