运用JavaBean封装数据库,简单的JSP登入界面,我在看代码之前,先说下用到的文件,总共会用到5个文件。
1.index.jsp:登入成功界面(一个空文件) 2.login.jsp:登入界面 3. LoginACtion.jsp :登入判断,中间跳转界面
4.usres.java:用户名注册界面 5.ORMDBUtil.java:后台连接数据库
1.login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="com.po.usres" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<style type="text/css">
.thred{
}
</style>
<script type="text/javascript">
function on_submit(){
if((form1.username.value!="admin")||(form1.userpassword.value!="admin"))
{
alter("账户或密码错误");
form1.username.focus();
return false;
}
</script>
</head>
<body>
<center> <form method="post" action="LoginAction.jsp" name="form1" οnsubmit="return on_submit">
<table>
<thead class=".thred" bgcolor="#666666">学生选课管理系统后台管理登入</thead>
<tr><td><label>用户名:</label> <input type="text" name="username" placeholder="评请输入账号"></td></tr>
<tr><td><label>密 码:</label> <input type="password" name="userpassword" placeholder="评请输入密码"></td></tr>
<tr><td><input type="submit" value="登入"></td>
<td><input type="reset" value="重置"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
2.LoginACtion.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.sql.*" %>
<jsp:useBean id="db" class="com.po.ORMDBUtil" scope="page"></jsp:useBean>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String name=request.getParameter("username");
String password=request.getParameter("userpassword");
String sql="select * from admin where username='"+name+"'and password= '"+password+"'";
ResultSet rs=db.Query(sql);
if(rs.next())
response.sendRedirect("index.jsp");
else
response.sendRedirect("login.jsp");
%>
</body>
</html>
3.usres.java
package com.po;
public class usres {
private String username ;
private String password ;
public usres(){}
{}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4.ORMDBUtil.java
package com.po;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class ORMDBUtil {
/**
*
* @return
* @throws SQLException
* @throws ClassNotFoundException
* @throws ClassNotFoundException
*/
public Connection getConnection() throws SQLException,
InstantiationException,IllegalAccessException,
ClassNotFoundException{
Connection conn = null;
Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url= "jdbc:sqlserver://localhost:1433;DatabaseName=student ";
String user = "sa";
String password = "12345";
conn = DriverManager.getConnection(url, user, password);
return conn;
}
/**
*
* @param sql
* @return
* @throws Exception
*/
@SuppressWarnings("rawtypes")
public ResultSet Query(String sql){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
} catch (InstantiationException | IllegalAccessException
| ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stmt = conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
}