package biz;
import java.util.List;
import model.Tab1509;
public interface Tab1509Biz {
int add(Tab1509 tab); //添加的方法
Tab1509 login(String name,int pwd); //登录的方法
List<Tab1509> getAll(); //得到所有的对象
}
package biz.impl;
import java.util.List;
import dao.Tab1509Dao;
import dao.impl.Tab1509DaoImpl;
import model.Tab1509;
import biz.Tab1509Biz;
public class Tab1509BizImpl implements Tab1509Biz{
Tab1509Dao td = new Tab1509DaoImpl();
@Override
public int add(Tab1509 tab) {
return td.add(tab);
}
@Override
public Tab1509 login(String name, int pwd) {
return td.login(name, pwd);
}
@Override
public List<Tab1509> getAll() {
return td.getAll();
}
}
package dao;
import java.util.List;
import model.Tab1509;
public interface Tab1509Dao {
int add(Tab1509 tab); //添加的方法
Tab1509 login(String name,int pwd); //登录的方法
List<Tab1509> getAll(); //得到所有的对象
}
package dao.impl;
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.BaseDao;
import model.Tab1509;
import dao.Tab1509Dao;
public class Tab1509DaoImpl extends BaseDao implements Tab1509Dao{
@Override
public int add(Tab1509 tab) {
Connection conn = this.getConn();
PreparedStatement ps = null;
int result=0; //返回结果
try {
String sql = "insert into tab1509 (name,age) values(?,?)"; //添加的SQL语句
ps = conn.prepareStatement(sql); //创建执行SQL语句的对象
ps.setString(1, tab.getName()); //设置问号1
ps.setInt(2, tab.getAge());//设置问号2
result = ps.executeUpdate(); //执行添加操作
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeAll(conn, ps, null); //释放资源
}
return result;
}
@Override
public Tab1509 login(String name, int pwd) {
Connection conn = this.getConn();
PreparedStatement ps = null;
Tab1509 tab = null;
ResultSet result=null; //返回结果
try {
String sql = "select * from tab1509 where name=? and age=?"; //添加的SQL语句
ps = conn.prepareStatement(sql); //创建执行SQL语句的对象
ps.setString(1, name); //设置问号1
ps.setInt(2,pwd);//设置问号2
result = ps.executeQuery(); //执行添加操作
if(result.next()){
tab = new Tab1509();
tab.setName(result.getString("name"));
tab.setAge(result.getInt("age"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeAll(conn, ps, result); //释放资源
}
return tab;
}
@Override
public List<Tab1509> getAll() {
List<Tab1509> list = new ArrayList<>();
Connection conn = this.getConn();
PreparedStatement ps = null;
ResultSet result=null; //返回结果
try {
String sql = "select * from tab1509"; //添加的SQL语句
ps = conn.prepareStatement(sql); //创建执行SQL语句的对象
result = ps.executeQuery(); //执行添加操作
while(result.next()){
Tab1509 tab = new Tab1509();
tab.setName(result.getString("name"));
tab.setAge(result.getInt("age"));
tab.setId(result.getInt("id"));
list.add(tab);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeAll(conn, ps, result); //释放资源
}
return list;
}
}
package model;
public class Tab1509 {
private int id; //编号
private String name; //姓名
private int age; //密码
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
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 model.Tab1509;
import biz.Tab1509Biz;
import biz.impl.Tab1509BizImpl;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求的编码格式
request.setCharacterEncoding("utf-8");
//设置响应的编码格式
response.setContentType("text/html,charset=utf-8");
//获得用户名和密码
String name = request.getParameter("uname");
int pwd =Integer.parseInt(request.getParameter("pwd"));
//调用业务逻辑层的登录的方法
Tab1509Biz tb = new Tab1509BizImpl();
Tab1509 tab = tb.login(name, pwd); //调用登录的方法返回了一个对象
if(tab!=null){
//跳到成功的页面
//把这个对象存在request里
request.setAttribute("user", tab);
//调用业务逻辑层查询所有用户信息的方法
List<Tab1509> list = tb.getAll();
//把list也存在request里
request.setAttribute("list", list);
//转发到成功页面
request.getRequestDispatcher("success.jsp").forward(request, response);
}else{
//跳回到登录页面
response.sendRedirect("login.html");
}
}
}
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 连接数据库的一个工具类
* @author Administrator
*
*/
public class BaseDao {
/**
* 获得与数据库的连接
* @return
*/
public Connection getConn(){
Connection conn = null;
//加载驱动程序到内存中
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:d:\\romsemary.db");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 释放资源
* @param conn
* @param ps
* @param rs
*/
public void closeAll(Connection conn,PreparedStatement ps,ResultSet rs){
try{
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
}