考试时用的是mysql数据库,没有完成建表和连接,现在装了sql server2012,按照http://blog.csdn.net/stewen_001/article/details/19553173/这篇博客进行了一些配置实现数据库的连接。考试时登录界面的代码如下
<!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>test</title> </head> <body> <center> <h1>登录</h1> <br><br><br><br> <form name="first" action="land.php" method="post"> <table width="500" border="0" cellspacing="20" cellpadding="0"> <tr> <td>用户名:</td> <td><input type="text" name="ures"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="ps"></td> </tr> <tr> </tr> <tr> <td colspan="3" align="center"><input type="submit" value="登录"、><!--注册按钮--> <input type="reset" value="取消"><!--取消按钮--> </tr> </table> </form> <br><br><br> <h2>忘记密码</a></h2> </tr> </table> </form> <br><br><br> <h3>没有账号?请先注册</a></h3> </center> </body> </html>
用了.html文件,此外还有一个.html文件是登录成功的界面,更加简单,此处不再粘贴代码。
需要一个.jsp文件实现页面跳转,判断等功能。
<%@ page language="java" contentType="text/html; charset= UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="css/Styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<meta http-equiv= "Content-Type" content="text/html; charset=UTF-8" >
<title>判断界面</title>
<script language="javascript">
$(function(){
$('.error').css({'position':'absolute','left':($(window).width()-490)/2});
$(window).resize(function(){
$('.error').css({'position':'absolute','left':($(window).width()-490)/2});
})
});
</script>
</head>
<body style="background:#edf6fa;">
<%
request.setCharacterEncoding("UTF-8");
String pws=request.getParameter("password");
String username=request.getParameter("username");
%>
<jsp:useBean id="db" class="DB.DBBean" scope="page" />
<%
if(username!=""&&password!="")
{
String sql="select * from Table_Login where 用户名='"+username+"'";
try{
ResultSet rs=db.executeQuery(sql);
if(rs.next())
{
if(pws.equals(rs.getString("密码")))
{
response.sendRedirect("deng.html");
}
else{
out.println( "<div class="error"> <h2>用户名和密码不匹配</h2> <div class="reindex"><a href=login.html target=parent>返回首页</a></div></div>");
return;
}
}
%>
</body>
</html>
.java文件用来连接数据库
import java.sql.*;
public class DBBean {
private String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String connStr = "jdbc:sqlserver://localhost:1433; DatabaseName=Test";
private String dbusername = "sa";
private String dbpassword = "zxw999@";
private Connection conn = null;
private Statement stmt = null;
public DBBean() {
try {
Class.forName(driverStr);
conn = DriverManager.getConnection(connStr, dbusername, dbpassword);
stmt = conn.createStatement();
} catch (Exception ex) {
System.err.println("连接数据库失败
错误原因: "+ex.getMessage() );
}
}
public int executeUpdate(String s) {
int result = 0;
try {
result = stmt.executeUpdate(s);
}
catch (Exception ex)
{
System.err.println("执行更新错误
错误原因: "+ex.getMessage());
}
return result;
}
public ResultSet executeQuery(String s) {
ResultSet rs = null;
try {
rs = stmt.executeQuery(s);
}
catch (Exception ex) {
System.err.println("执行查询错误ִ
错误原因: "+ex.getMessage());
}
return rs;
}
public void close() {
try {
stmt.close();
conn.close();
}
catch (Exception e) {
System.err.println("关闭失败ִ
错误原因: "+e.getMessage());
}
System.out.println("已断开与数据库的连接!" );
}
}