具备的测试工具:Eclipse,MySQL数据库,HBuilder,jstl1.2jar,mysql.connector.java.8.0.jar包
jstl1.2jar下载地址:https://pan.baidu.com/s/1T23zxoEg3jlZHFikrbWtHw
mysql.connector.java.8.0.jar包下载地址:https://pan.baidu.com/s/1kqfOuxo3nOT--CTrlp1BmA
首先打开Eclipse创建一个动态Web项目(Dynamic Web Project),在Src下分别创建以下包
其中web其实就是对应action层,这就是典型的MVC模型,这是属于后端
首先创建与你数据库的表结构对应的实体类
1、我的数据库结构如下
建立数据库
create database LoveDB;
建立数据表LoveInHeart
use LoveDB;
create table LoveInHeart(
lid int auto_increment primary key,
lname varchar(50) not null,
lsex varchar(50) not null,
ltime date not null,
lmoney float
);
然后可以向里面添加一些测试数据
insert into LoveInHeart(lname,lsex,ltime,lmoney)
values ('廖远平','男','1999-02-15',1000000);
insert into LoveInHeart(lname,lsex,ltime,lmoney)
values ('彭桂妮','女','1998-8-19',1000);
insert into LoveInHeart(lname,lsex,ltime,lmoney)
values ('科比','男','2008-05-05',100);
然后实体类的属性参数要和数据库的表结构保持一致,注:实体类是放在entity包下
声明变量以后进行封装,右键选择 source --> Getter and Setter ,然后select All 点击完成
接下来创建与数据库链接的DatabaseConnection类来完成与数据库的交互,在dao包下新建一个类,名字为:BaseDB
package com.nf.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class BaseDao { private static final String DRIVER_CLASS = "com.mysql.cj.jdbc.Driver"; private static final String SERVER_IP = "localhost";// 服务器IP地址 private static final String DATABASE_NAME = "lovedb";// 连接到哪一个数据库 private static final String USER_NAME = "root";// 用户名 private static final String PASSWORD = "123456";// 密码 public Connection getConnection() { Connection conn = null; String jdbcUrl = "jdbc:mysql://" + SERVER_IP + ":3306/" + DATABASE_NAME + "?serverTimezone=Asia/Shanghai&useSSL=true"; try { Class.forName(DRIVER_CLASS); conn = DriverManager.getConnection(jdbcUrl, USER_NAME, PASSWORD); } catch (Exception e) { System.out.println("获取连接时,异常" + e.getMessage()); conn = null; } return conn; } public void closeAll(Connection conn, PreparedStatement pst, ResultSet rs) { try { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
在dao层下面创建一个Interface类,声明三个方法(查询、添加、删除),名字:LoveInHeartDao
package com.nf.dao; import java.util.List; import com.nf.entity.LoveInHeart; import com.nf.util.MyPage; public interface LoveInHeartDao{ public List<LoveInHeart> getAll(MyPage myPage); public boolean add(LoveInHeart love); public boolean delete(int id); }
然后创建一个实现接口class类(Implments),名字:LoveInHeart_Impl
package com.nf.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 com.nf.entity.LoveInHeart; import com.nf.util.MyPage; public class LoveInHeartDaoImpl extends BaseDao implements LoveInHeartDao { @Override //查询 public List<LoveInHeart> getAll(MyPage myPage) { // 操作数据库,需要一个连接Connection Connection connection = getConnection(); PreparedStatement prepareStatement = null; ResultSet resultSet = null; List<LoveInHeart> loveInHeartList = new ArrayList(); String sql_data = "select * from loveinheart order by lid desc "; String sql_recodeCount = "select count(1) as mycount from loveinheart"; sql_data += "limit "; sql_data += (myPage.getCurrentPage()-1)*myPage.getPerPageSize(); sql_data += ","; sql_data += myPage.getPerPageSize(); System.out.println("分页的SQL:"+sql_data); try { prepareStatement = connection.prepareStatement(sql_data); resultSet = prepareStatement.executeQuery(); while (resultSet.next()) { LoveInHeart l = new LoveInHeart(); l.setLid(resultSet.getInt("lid")); l.setLname(resultSet.getString("lname")); l.setLsex(resultSet.getString("lsex")); l.setLtime(resultSet.getDate("ltime")); l.setLmoney(resultSet.getFloat("lmoney")); loveInHeartList.add(l); } } catch (SQLException e) { e.printStackTrace(); } try { prepareStatement = connection.prepareStatement(sql_recodeCount); resultSet = prepareStatement.executeQuery(); resultSet.next(); myPage.setRecodeCount( resultSet.getInt("mycount") ); } catch (SQLException e) { e.printStackTrace(); } closeAll(connection, prepareStatement, resultSet); // System.out.println(connection); return loveInHeartList; } @Override //添加 public boolean add(LoveInHeart love) { // 操作数据库,需要一个连接Connection Connection connection = getConnection(); PreparedStatement prepareStatement = null; try { prepareStatement = connection .prepareStatement("insert into LoveInHeart(lname,lsex,ltime,lmoney) values (?,?,?,?)"); prepareStatement.setString(1, love.getLname()); prepareStatement.setString(2, love.getLsex()); prepareStatement.setDate(3, love.getLtime()); prepareStatement.setFloat(4, love.getLmoney()); prepareStatement.executeUpdate(); closeAll(connection, prepareStatement, null); return true; } catch (SQLException e) { e.printStackTrace(); return false; } } @Override //删除 public boolean delete(int id) { // 操作数据库,需要一个连接Connection Connection connection = getConnection(); PreparedStatement prepareStatement = null; try { prepareStatement = connection .prepareStatement("delete from LoveInHeart where lid=?"); prepareStatement.setInt(1, id); prepareStatement.executeUpdate(); closeAll(connection, prepareStatement, null); return true; } catch (SQLException e) { e.printStackTrace(); return false; } } }
接下来在Service层再创建一个interface类,名字:LoveInHeartService
package com.nf.service; import java.util.List; import com.nf.entity.LoveInHeart; import com.nf.entity.MyData; public interface LoveInHeartService { public MyData getAll(int currentPage); public boolean add(LoveInHeart love); public boolean delete(int id); }
随机在同Service层下实现该接口