1: public 修饰的类 , public 无参构造
2: 所有属性都是private,并且提供 get/set 方法
( 1 ):封装业务逻辑的JavaBean:可以将 jsp 中的 JDBC 代码,封装到Login.java类中。
数据库数据:
web项目:
index.jsp :
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="check.jsp" method="post">
用户名:<input type="text" name="uname" /><br/>
密码:<input type="password" name="upwd"/><br/>
<input type="submit" value="登录" /> <br/>
</form>
</body>
</html>
check.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="org.lanqiao.dao.LoginDao" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String name = request.getParameter("uname");
String pwd = request.getParameter("upwd");
LoginDao dao = new LoginDao();
int count = dao.login(name, pwd);
if(count==1) out.print("登录成功!");
else if(count==0) out.print("用户名或密码错误!");
else out.print("出现异常!");
%>
</body>
</html>
LoginDao.java:
package org.lanqiao.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class LoginDao {
public int login(String name , String pwd)
{
String URL="jdbc:mysql://127.0.0.1:3306/jdbc?serverTimezone=UTC&characterEncoding=UTF-8";
String USERNAME = "root"; //账号
String PWD = "123456"; //密码
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
//导入驱动,加载具体的驱动类
Class.forName("com.mysql.cj.jdbc.Driver");
//连接数据库
conn = DriverManager.getConnection(URL,USERNAME,PWD);
//发送sql语句
// String sql = "select count(*) from login where name = ? and pwd = ?"; //报错,找不到原因
String sql = "select * from login";
pstmt = conn.prepareStatement(sql);
// pstmt.setString(1, name);
// pstmt.setString(2, pwd);
//执行查询,获取结果集
rs = pstmt.executeQuery(sql);
//处理结果集
int count=0;
while(rs.next()){
String daoName = rs.getString("name");
String daoPwd = rs.getString("pwd");
if(name.equals(daoName) && pwd.equals(daoPwd)){
count = 1;
break;
}
}
return count;
}catch(Exception e){
e.printStackTrace();
return -1;
}finally{
try {
//关闭连接
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
( 2 ):封装数据的JavaBean (实体类 student.java):
Login login = new Login(name,pwd) 用Login对象,封装两个数据
(1)记得在check.jsp中导包:
<%@ page import="userLogin.Login" %>
(2)check中代码修改:
int count = dao.login(new Login(name,pwd));
封装一个javabean : Login.java (封装 name,pwd)
package userLogin;
public class Login {
private String name;
private String pwd;
public Login() { //无参构造
super();
// TODO Auto-generated constructor stub
}
public Login(String name, String pwd) { //有参构造
super();
this.name = name;
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
稍作修改LoginDao.java (把参数用javabean代替)
package org.lanqiao.dao;
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 userLogin.Login;
public class LoginDao {
public int login(Login login) //使用bean代替数据
{
String URL="jdbc:mysql://127.0.0.1:3306/jdbc?serverTimezone=UTC&characterEncoding=UTF-8";
String USERNAME = "root"; //账号
String PWD = "123456"; //密码
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
//导入驱动,加载具体的驱动类
Class.forName("com.mysql.cj.jdbc.Driver");
//连接数据库
conn = DriverManager.getConnection(URL,USERNAME,PWD);
//发送sql语句
// String sql = "select count(*) from login where name = ? and pwd = ?"; //报错,找不到原因
String sql = "select * from login";
pstmt = conn.prepareStatement(sql);
// pstmt.setString(1, name);
// pstmt.setString(2, pwd);
//执行查询,获取结果集
rs = pstmt.executeQuery(sql);
//处理结果集
int count=0;
while(rs.next()){
String daoName = rs.getString("name");
String daoPwd = rs.getString("pwd");
if(login.getName().equals(daoName) && login.getPwd().equals(daoPwd)){
count = 1;
break;
}
}
return count;
}catch(Exception e){
e.printStackTrace();
return -1;
}finally{
try {
//关闭连接
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
封装数据的JavaBean 对应于数据库中的一张表 (Login(name,pwd))
封装业务逻辑的JavaBean 用于操作一个封装数据的JavaBean