package com;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ParamServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置请求和响应的编码
request.setCharacterEncoding(“utf-8”);
response.setCharacterEncoding(“utf-8”);
//获取客户端参数
String name = request.getParameter(“name”);
//获取输出流
PrintWriter out = response.getWriter();
if(name==null){
out.println(“not name exist”);
}else{
out.println(name);
}
}
}
package dao;
import java.util.List;
import entity.Account;
public interface AccountDao {
//插入一条数据
public void insertAccount(Account acc);
//更新一条数据
public void updateAccount(Account acc);
//根据card_id删除一条信息
public void deleteAccount(Integer card_id);
//查询所有信息
public List queryAccount();
//根据card_id查询用户信息
public Account queryByIdAccount(Integer card_id);
}
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import util.JDBCUtil3;
import entity.Account;
public class AccountDaoImpl implements AccountDao{
public void insertAccount(Account acc) {
Connection conn = null;
PreparedStatement pstm =null;
try {
conn = JDBCUtil3.getConnection();
//3.创建PreparedStatement
pstm = conn.prepareStatement("insert into account values(account_seq.nextval,?,?,?,?)");
pstm.setString(1, acc.getUsername());
pstm.setString(2, acc.getPassword());
pstm.setDouble(3, acc.getBalance());
pstm.setString(4, acc.getMobile());
//4.执行sql
pstm.executeUpdate();
//5.处理结果集
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
JDBCUtil3.close(null, pstm, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void updateAccount(Account acc) {
Connection conn = null;
PreparedStatement pstm = null;
try {
conn = JDBCUtil3.getConnection();
//3.创建PreparedStatement
pstm = conn.prepareStatement("update account set username=?,password=?," +
"balance=?," +
"mobile=? where card_id=?");
pstm.setString(1, acc.getUsername());
pstm.setString(2, acc.getPassword());
pstm.setDouble(3, acc.getBalance());
pstm.setString(4, acc.getMobile());
pstm.setInt(5, acc.getCard_id());
//4.执行sql
pstm.executeUpdate();
//5.处理结果集
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
JDBCUtil3.close(null, pstm, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void deleteAccount(Integer card_id) {
Connection conn =null;
//3.创建PreparedStatement
PreparedStatement pstm=null;
try {
conn = JDBCUtil3.getConnection();
pstm = conn.prepareStatement("delete from account where card_id=?");
pstm.setInt(1, card_id);
//4.执行sql
pstm.executeUpdate();
//5.处理结果集
} catch (Exception e) {
// TODO: handle exception
}finally{
try {
JDBCUtil3.close(null, pstm, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public List<Account> queryAccount() {
Connection conn = null;
//3.创建PreparedStatement
PreparedStatement pstm = null;
//4.执行sql
ResultSet rs = null;
//创建一个List<Account>
List<Account> list = null;
try {
conn = JDBCUtil3.getConnection();
pstm = conn.prepareStatement("select * from account");
rs = pstm.executeQuery();
list = new ArrayList<Account>();
//声明一个Account 变量
Account acc = null;
//5.处理结果集
while(rs.next()){
acc = new Account();
acc.setCard_id(rs.getInt(1));
acc.setUsername(rs.getString(2));
acc.setPassword(rs.getString(3));
acc.setBalance(rs.getDouble(4));
acc.setMobile(rs.getString(5));
list.add(acc);
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
JDBCUtil3.close(rs, pstm, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
public Account queryByIdAccount(Integer card_id) {
Connection conn = null;
//3.创建PreparedStatement
PreparedStatement pstm = null;
//4.执行sql
ResultSet rs =null;
//5.处理结果集
Account a=null;
try {
conn = JDBCUtil3.getConnection();
pstm = conn.prepareStatement("select * from account where card_id=?");
pstm.setInt(1, card_id);
rs = pstm.executeQuery();
while(rs.next()){
a = new Account();
a.setCard_id(rs.getInt(1));
a.setUsername(rs.getString(2));
a.setPassword(rs.getString(3));
a.setBalance(rs.getDouble(4));
a.setMobile(rs.getString(5));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
JDBCUtil3.close(rs, pstm, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return a;
}
}
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import util.JDBCUtil3;
import entity.Account;
public class AccountDaoImpl implements AccountDao{
public void insertAccount(Account acc) {
Connection conn = null;
PreparedStatement pstm =null;
try {
conn = JDBCUtil3.getConnection();
//3.创建PreparedStatement
pstm = conn.prepareStatement("insert into account values(account_seq.nextval,?,?,?,?)");
pstm.setString(1, acc.getUsername());
pstm.setString(2, acc.getPassword());
pstm.setDouble(3, acc.getBalance());
pstm.setString(4, acc.getMobile());
//4.执行sql
pstm.executeUpdate();
//5.处理结果集
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
JDBCUtil3.close(null, pstm, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void updateAccount(Account acc) {
Connection conn = null;
PreparedStatement pstm = null;
try {
conn = JDBCUtil3.getConnection();
//3.创建PreparedStatement
pstm = conn.prepareStatement("update account set username=?,password=?," +
"balance=?," +
"mobile=? where card_id=?");
pstm.setString(1, acc.getUsername());
pstm.setString(2, acc.getPassword());
pstm.setDouble(3, acc.getBalance());
pstm.setString(4, acc.getMobile());
pstm.setInt(5, acc.getCard_id());
//4.执行sql
pstm.executeUpdate();
//5.处理结果集
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
JDBCUtil3.close(null, pstm, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void deleteAccount(Integer card_id) {
Connection conn =null;
//3.创建PreparedStatement
PreparedStatement pstm=null;
try {
conn = JDBCUtil3.getConnection();
pstm = conn.prepareStatement("delete from account where card_id=?");
pstm.setInt(1, card_id);
//4.执行sql
pstm.executeUpdate();
//5.处理结果集
} catch (Exception e) {
// TODO: handle exception
}finally{
try {
JDBCUtil3.close(null, pstm, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public List<Account> queryAccount() {
Connection conn = null;
//3.创建PreparedStatement
PreparedStatement pstm = null;
//4.执行sql
ResultSet rs = null;
//创建一个List<Account>
List<Account> list = null;
try {
conn = JDBCUtil3.getConnection();
pstm = conn.prepareStatement("select * from account");
rs = pstm.executeQuery();
list = new ArrayList<Account>();
//声明一个Account 变量
Account acc = null;
//5.处理结果集
while(rs.next()){
acc = new Account();
acc.setCard_id(rs.getInt(1));
acc.setUsername(rs.getString(2));
acc.setPassword(rs.getString(3));
acc.setBalance(rs.getDouble(4));
acc.setMobile(rs.getString(5));
list.add(acc);
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
JDBCUtil3.close(rs, pstm, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
public Account queryByIdAccount(Integer card_id) {
Connection conn = null;
//3.创建PreparedStatement
PreparedStatement pstm = null;
//4.执行sql
ResultSet rs =null;
//5.处理结果集
Account a=null;
try {
conn = JDBCUtil3.getConnection();
pstm = conn.prepareStatement("select * from account where card_id=?");
pstm.setInt(1, card_id);
rs = pstm.executeQuery();
while(rs.next()){
a = new Account();
a.setCard_id(rs.getInt(1));
a.setUsername(rs.getString(2));
a.setPassword(rs.getString(3));
a.setBalance(rs.getDouble(4));
a.setMobile(rs.getString(5));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
JDBCUtil3.close(rs, pstm, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return a;
}
}
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import util.JDBCUtil3;
import entity.User;
public class UserDaoImpl implements UserDao{
public User queryByUsernameAndPwd(String username, String password) {
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
try{
//获取链接
conn = JDBCUtil3.getConnection();
pstm = conn.prepareStatement("select * from test_user " +
"where username=? and password=?");
pstm.setString(1, username);
pstm.setString(2, password);
rs = pstm.executeQuery();
User user = null;
while(rs.next()){
user = new User(rs.getString(1),rs.getString(2));
}
return user;
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException("登陆失败 用户名或密码错误");
}finally{
try {
JDBCUtil3.close(rs, pstm, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package entity;
import java.io.Serializable;
public class Account implements Serializable{
private Integer card_id;
private String username;
private String password;
private Double balance;
private String mobile;
public Integer getCard_id() {
return card_id;
}
public void setCard_id(Integer card_id) {
this.card_id = card_id;
}
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;
}
public Double getBalance() {
return balance;
}
public void setBalance(Double balance) {
this.balance = balance;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public Account() {
super();
// TODO Auto-generated constructor stub
}
public Account(Integer card_id, String username, String password,
Double balance, String mobile) {
super();
this.card_id = card_id;
this.username = username;
this.password = password;
this.balance = balance;
this.mobile = mobile;
}
@Override
public String toString() {
return “Account [card_id=” + card_id + “, username=” + username
+ “, password=” + password + “, balance=” + balance
+ “, mobile=” + mobile + “]”;
}
}
package entity;
import java.io.Serializable;
public class User implements Serializable{
private String username;
private String password;
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;
}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return “User [username=” + username + “, password=” + password + “]”;
}
}
package service;
import java.util.List;
import entity.Account;
public interface AccountService {
//转账操作
public void transfer(Integer fromCard,String password,
Integer toCard,Double money);
//插入一条数据
public void insertAccount(Account acc);
//更新一条数据
public void updateAccount(Account acc);
//根据card_id删除一条信息
public void deleteAccount(Integer card_id);
//查询所有信息
public List queryAccount();
//根据card_id查询用户信息
public Account queryByIdAccount(Integer card_id);
}
package service;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import util.JDBCUtil3;
import dao.AccountDao;
import dao.AccountDaoImpl;
import dao.UserDao;
import dao.UserDaoImpl;
import entity.Account;
import entity.User;
public class AccountServiceImpl implements AccountService{
public void insertAccount(Account acc) {
Connection conn = null;
try{
conn = JDBCUtil3.getConnection();
conn.setAutoCommit(false);
//调用dao
AccountDao ud = new AccountDaoImpl();
ud.insertAccount(acc);
conn.commit();
}catch(Exception e){
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
throw new RuntimeException("插入失败");
}finally{
try {
JDBCUtil3.close(null, null, conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void updateAccount(Account acc) {
Connection conn = null;
try{
conn = JDBCUtil3.getConnection();
conn.setAutoCommit(false);
//调用dao
AccountDao ud = new AccountDaoImpl();
ud.updateAccount(acc);
conn.commit();
}catch(Exception e){
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
throw new RuntimeException("更新失败");
}finally{
try {
JDBCUtil3.close(null, null, conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void deleteAccount(Integer card_id) {
Connection conn = null;
try{
conn = JDBCUtil3.getConnection();
conn.setAutoCommit(false);
//调用dao
AccountDao ud = new AccountDaoImpl();
ud.deleteAccount(card_id);
conn.commit();
}catch(Exception e){
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
throw new RuntimeException("删除失败");
}finally{
try {
JDBCUtil3.close(null, null, conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public List<Account> queryAccount() {
Connection conn = null;
try{
conn = JDBCUtil3.getConnection();
conn.setAutoCommit(false);
//调用dao
AccountDao ud = new AccountDaoImpl();
List<Account> list = ud.queryAccount();
conn.commit();
return list;
}catch(Exception e){
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
throw new RuntimeException("查询失败");
}finally{
try {
JDBCUtil3.close(null, null, conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public Account queryByIdAccount(Integer card_id) {
Connection conn = null;
try{
conn = JDBCUtil3.getConnection();
conn.setAutoCommit(false);
//调用dao
AccountDao ud = new AccountDaoImpl();
Account account = ud.queryByIdAccount(card_id);
conn.commit();
return account;
}catch(Exception e){
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
throw new RuntimeException("查询失败");
}finally{
try {
JDBCUtil3.close(null, null, conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void transfer(Integer fromCard, String password, Integer toCard,
Double money) {
//获取一个Connection
Connection conn = null;
try {
conn = JDBCUtil3.getConnection();
//设置手动事务
conn.setAutoCommit(false);
//1.验证转账账户
AccountDao ad = new AccountDaoImpl();
Account acc = ad.queryByIdAccount(fromCard);
if (acc == null) {
throw new RuntimeException("用户不存在!~");
}
if (!password.equals(acc.getPassword())) {
throw new RuntimeException("用户密码错误!~");
}
//2.更新转账账户余额 -- 判断转账金额 是否大于当前余额
if (money > acc.getBalance()) {
throw new RuntimeException("余额不足!~");
}
Double newBalance = acc.getBalance() - money;
acc.setBalance(newBalance);
ad.updateAccount(acc);
System.out.println("更新转账账户余额成功!~");
//3.验证对方的账户
Account toAcc = ad.queryByIdAccount(toCard);
if (toAcc == null) {
throw new RuntimeException("对方账户不存在!·");
}
//4.更新对方账户余额
Double toBalance = toAcc.getBalance() + money;
toAcc.setBalance(toBalance);
ad.updateAccount(toAcc);
System.out.println("转账成功~");
//手动提交事务
conn.commit();
} catch (Exception e) {
//手动回滚事务
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
throw new RuntimeException(e);
}finally{
try {
JDBCUtil3.close(null, null, conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package service;
import entity.User;
public interface UserService {
//验证登陆
public User doLogin(String username,String password);
}
package service;
import java.sql.Connection;
import java.sql.SQLException;
import util.JDBCUtil3;
import dao.UserDao;
import dao.UserDaoImpl;
import entity.User;
public class UserServiceImpl implements UserService{
public User doLogin(String username, String password) {
Connection conn = null;
try{
conn = JDBCUtil3.getConnection();
conn.setAutoCommit(false);
//调用dao
UserDao ud = new UserDaoImpl();
User user = ud.queryByUsernameAndPwd(username, password);
conn.commit();
return user;
}catch(Exception e){
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
throw new RuntimeException("登陆失败");
}finally{
try {
JDBCUtil3.close(null, null, conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class A extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 从A servlet – B servlet
PrintWriter out = response.getWriter();
out.println("this is a servlet");
//向request作用域存入数据
request.setAttribute("rname", "rvalue");
/**
* RequestDispatcher 他的作用就是 指定你要跳转(转发)到哪个servlet
*/
System.out.println("this is a servlet");
//将请求转发到B servlet
RequestDispatcher rd = request.getRequestDispatcher("/B");
rd.forward(request, response);
}
}
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.AccountService;
import service.AccountServiceImpl;
import entity.Account;
public class AddAction extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.设置编码格式 获取客户端的数据
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String balance = request.getParameter("balance");
String mobile = request.getParameter("mobile");
//将需要处理的数据进行转换
double b = Double.parseDouble(balance);
//2.调用业务功能 service
AccountService as = new AccountServiceImpl();
as.insertAccount(new Account(null,username,password,b,mobile));
//3.页面跳转
response.sendRedirect("/servletday2/queryAction");
}
}
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AddView extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置响应的类型 编码格式
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
//获得输出流
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<form action='/servletday2/addAction' method='post'>");
out.println("用户名:<input type='text' name='username'><br/>");
out.println("密码:<input type='password' name='password'><br/>");
out.println("余额:<input type='text' name='balance'><br/>");
out.println("电话:<input type='text' name='mobile'><br/>");
out.println("<input type='submit' value='提交'>");
out.println("<input type='reset' value='重置'><br/>");
out.println("</form>");
out.println("</body></html>");
out.flush();
}
}
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class B extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(“this is b servlet”);
//从request作用域取出一个命名属性值
String rvalue = (String)request.getAttribute(“rname”);
//获取输出流
PrintWriter out = response.getWriter();
out.println("B servlet:"+rvalue);
}
}
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
-
类的作用:实现登陆验证
*/
public class LoginAction extends HttpServlet{
public LoginAction(){
System.out.println(“创建了 loginAction”);
}
public void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
//设置编码
request.setCharacterEncoding(“utf-8”);
response.setCharacterEncoding(“utf-8”);
//设置响应类型
response.setContentType(“text/html”);//设置一个 cookie Cookie[] cookies = request.getCookies(); if(cookies==null){ Cookie cookie = new Cookie("username","password"); cookie.setMaxAge(1200); response.addCookie(cookie); } //获取客户端的参数 String username = request.getParameter("username"); String password = request.getParameter("password"); //如果 用户名和密码 都是 baizhi 那么 向页面输出 登陆成功 否则输出登陆失败 //获取输出流 PrintWriter out = response.getWriter(); if("baizhi".equals(username) && "baizhi".equals(password)){ //页面跳转 response.sendRedirect("/servletday2/queryAction");
// out.println(“登陆成功!~”);
}else{
out.println(“登陆失败!~”);
}
out.flush();
}
}
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.UserService;
import service.UserServiceImpl;
import entity.User;
public class LoginUserAction extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置编码
request.setCharacterEncoding(“utf-8”);
response.setCharacterEncoding(“utf-8”);
//设置响应类型
response.setContentType(“text/html”);
//获取客户端参数
String username = request.getParameter(“username”);
String password = request.getParameter(“password”);
//调用service
UserService us = new UserServiceImpl();
User user = us.doLogin(username, password);
//获取输出流
PrintWriter out = response.getWriter();
if(user == null){
out.println("用户名或密码错误!~");
}else{
out.println("登陆成功,欢迎~"+user.getUsername());
}
out.flush();
}
}
package servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Account;
import service.AccountService;
import service.AccountServiceImpl;
public class QueryAction extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1.调用业务功能(获取客户端的数据)
AccountService as = new AccountServiceImpl();
List<Account> list = as.queryAccount();
//2.向request作用域存入数据
request.setAttribute("list", list);
//3.页面跳转
request.getRequestDispatcher("/queryView").forward(request, response);
}
}
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Account;
public class QueryView extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
Cookie[] cookies = request.getCookies();
if(cookies!=null){
for (Cookie cookie : cookies) {
out.println(cookie.getName()+"="+cookie.getValue());
}
}
//1.到request中取出数据
List<Account> list = (List<Account>) request.getAttribute("list");
//2.获取输出流 向客户端输出结果
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
out.println("<html><body>");
out.println("<table border='2px' cellspacing='0' align='center'>");
//表头
out.println("<tr bgcolor='blue'>");
out.println("<td>CARD_ID</td>");
out.println("<td>USERNAME</td>");
out.println("<td>PASSWORD</td>");
out.println("<td>BALANCE</td>");
out.println("<td>MOBILE</td>");
out.println("<td>操作</td>");
out.println("</tr>");
//循环遍历list将数据显示在表格中
for(Account acc:list){
out.println("<tr>");
out.println("<td>"+acc.getCard_id()+"</td>");
out.println("<td>"+acc.getUsername()+"</td>");
out.println("<td>"+acc.getPassword()+"</td>");
out.println("<td>"+acc.getBalance()+"</td>");
out.println("<td>"+acc.getMobile()+"</td>");
out.println("<td>" +
"<a href=''>update</a> " +
"<a href=''>delete</a></td>");
out.println("</tr>");
}
out.println("</table>");
out.println("<center><a href='/servletday2/addView'>create account</a></center>");
out.println("</body></html>");
out.flush();
}
}
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Account;
import service.AccountService;
import service.AccountServiceImpl;
public class ShowAllAccountServlet extends HttpServlet{
public void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
//设置编码格式
request.setCharacterEncoding(“utf-8”);
response.setCharacterEncoding(“utf-8”);
//设置响应类型
response.setContentType(“text/html”);
//调用AccountService
AccountService as = new AccountServiceImpl();
List<Account> list = as.queryAccount();
//获得输出流 将结果响应到客户端
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<table border='2px' cellspacing='0' align='center'>");
//表头
out.println("<tr bgcolor='blue'>");
out.println("<td>CARD_ID</td>");
out.println("<td>USERNAME</td>");
out.println("<td>PASSWORD</td>");
out.println("<td>BALANCE</td>");
out.println("<td>MOBILE</td>");
out.println("</tr>");
//循环遍历list将数据显示在表格中
for(Account acc:list){
out.println("<tr>");
out.println("<td>"+acc.getCard_id()+"</td>");
out.println("<td>"+acc.getUsername()+"</td>");
out.println("<td>"+acc.getPassword()+"</td>");
out.println("<td>"+acc.getBalance()+"</td>");
out.println("<td>"+acc.getMobile()+"</td>");
out.println("</tr>");
}
out.println("</table>");
out.println("</body></html>");
out.flush();
}
}
package util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
/**
-
获取数据库的连接 Connection
*/
public class JDBCUtil3 {
//
private static Properties prop = new Properties();//静态初始代码块 – 只在类加载的时候 执行一次
static{
InputStream in = null;
//1.加载驱动
try {
//获得一个 保存有jdbc.properties文件内容的输入流
in = JDBCUtil3.class.getResourceAsStream("/jdbc.properties");
//将 输入流中的数据保存到Properties对象中
prop.load(in);
//通过 键获得值
System.out.println(prop.getProperty(“driver”));
Class.forName(prop.getProperty(“driver”));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(“加载驱动失败!~”);
}finally{
//关闭流
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static ThreadLocal tol = new ThreadLocal();
//获取一个Connection
public static Connection getConnection() throws Exception{
//从当前线程取Connection 如果没有就创建一个
Connection conn = tol.get();
if(conn == null){
//2.获取Connection
conn = DriverManager.getConnection(prop.getProperty(“url”),
prop.getProperty(“username”),prop.getProperty(“password”));
tol.set(conn);
}return conn;
}
//释放资源
public static void close(ResultSet rs,Statement stm,Connection conn) throws Exception{if(rs!=null) { rs.close(); } if(stm!=null) { stm.close(); } if(conn!=null) { conn.close(); tol.remove(); }
}
}