今天项目内容已经开始了,并且已经完成好多基本操作,今天就开始总结今天学习到的内容,和我遇到的问题,以及分析这其中的原因。

内容模块:

1:Java代码实现对数据库的增删查;

2:分页且获取页面信息;


这里针对于项目里面的Genre实体,以及对于它的操作进行举例

 package com.music.entity;

 public class Genre {
private int id;
private String name;
private String description; 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 String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
} }

Genre.java

逻辑层代码展示:

GenreDao:

 package com.music.Dao;

 import java.util.List;

 import com.music.entity.Genre;

 public interface GenreDao {
//查询
public List<Genre> getAll();
//删除
public boolean deleteGenre(int id);
//插入
public boolean addGenre(Genre g);
//更新
public boolean updateGenre(Genre g);
}

在这个接口里方法的具体实现GenreDaoImpl:

 package com.music.Dao.Impl;

 import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import com.music.Dao.GenreDao;
import com.music.entity.Genre; public class GenreDaoImpl extends BaseDao implements GenreDao{ //保存获取结果
ArrayList<Genre> genres = new ArrayList<Genre>();
@Override
public List<Genre> getAll() {
try {
//创建连接
openConnection();
String sql = "select * from genre";
//执行查询,获取结果
ResultSet resultSet = executeQuery(sql, null);
//将查询结果转换成对象
while (resultSet.next()) {
Genre g = new Genre();
g.setId(resultSet.getInt("id"));
g.setName(resultSet.getString("name"));
g.setDescription(resultSet.getString("description"));
genres.add(g);
}
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (SQLException e) { e.printStackTrace();
}finally{
closeResourse();
}
return genres;
} @Override
public boolean deleteGenre(int id) {
boolean result = false;
try {
openConnection();
String sql ="delete from genre where id = ?";
result = excute(sql, new Object[]{id});
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (SQLException e) { e.printStackTrace();
}finally{
closeResourse();
}
return result;
} @Override
public boolean addGenre(Genre g) {
boolean result = false;
try {
openConnection();
String sql ="insert into genre value(?,?,?)";
result =excute(sql, new Object[]{
g.getId(),
g.getDescription(),
g.getName()
});
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeResourse();
}
return result;
} @Override
public boolean updateGenre(Genre g) {
boolean result = false;
try {
openConnection();
String sql = "update genre set name = ?, description =? where id=?";
result = excute(sql, new Object[]{
g.getId()
});
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (SQLException e) { e.printStackTrace();
}finally{
closeResourse();
}
return result; } public static void main(String[] args) {
GenreDaoImpl genreDaoImpl = new GenreDaoImpl();
genreDaoImpl.getAll();
System.out.println(genreDaoImpl); } }

这里还必须要提出BaseDao:

 package com.music.Dao.Impl;

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class BaseDao {
//连接数据库
private String className = "com.mysql.jdbc.Driver";
private String dburl = "jdbc:mysql://localhost/ZJJ";
private String user = "root";
private String password = "root";
private Connection connection;
private PreparedStatement statement;
private ResultSet resultSet; public void openConnection() throws ClassNotFoundException, SQLException{
//加载驱动
Class.forName(className);
//创建连接
connection = DriverManager.getConnection(dburl,user,password);
} //查询方法
public ResultSet executeQuery(String sql,Object[] params) throws SQLException{
statement =connection.prepareStatement(sql);
//追加参数
if(params !=null){
int i=1;
for (Object object : params) {
statement.setObject(i, object);
i++;
}
}
resultSet =statement.executeQuery();
return resultSet;
} //更新
public boolean excute(String sql,Object[] params) throws SQLException {
statement =connection.prepareStatement(sql);
if(params !=null){
int i=1;
for (Object object : params) {
statement.setObject(i, object);
}
}
return statement.execute();
}
//释放资源
public void closeResourse(){
try {
if(resultSet != null){
resultSet.close();
}
if(statement != null){
statement.close();
}
if(connection != null){
connection.close();
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

这个里面的方法都会获得调用。

今天报了一个错误:

错误的原因是:

这个setObject(i,object)有一个好处就是将所有的对象类型都写成object,这样就可以不用分开写各个类型的方法,比较简便。


分页:

接口部分:

 //分页
public List<Album> getAlbumWithPage(int genreid,int pageNum,int pageSize);

1 public List<Album> getAlbumWithPage(int genreid, int pageNum, int pageSize)

实现部分:

 public List<Album> getAlbumWithPage(int genreid, int pageNum, int pageSize) {
ArrayList<Album> albums = new ArrayList<Album>();
//pageNum当前页数
try {
openConnection();
String sql= "select * from album where genreid =? limit ?,?";
ResultSet resultSet = executeQuery(sql, new Object[]{
genreid,
(pageNum-1)*pageSize,
pageSize
});
while (resultSet.next()) {
Album al= new Album();
al.setId(resultSet.getInt("id"));
al.setGenreid(resultSet.getInt("genreid"));
al.setArtist(resultSet.getString("artist"));
al.setTitle(resultSet.getString("title"));
al.setPrice(resultSet.getBigDecimal("price"));
al.setStock(resultSet.getInt("stock"));
al.setDateReleased(resultSet.getString("dateReleased"));
al.setDescription(resultSet.getString("description"));
albums.add(al);
}
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (SQLException e) { e.printStackTrace();
}
return albums;
 
1 public List<Album> getAlbumWithPage(int genreid, int pageNum, int pageSize) {

return daoImpl.getAlbumWithPage(genreid, pageNum, pageSize);
} @Override
public int getRowCountWithGenreid(int id) { return daoImpl.getAlbumWithGenreid(id).size();
}

JSP代码部分

 <%@page import="com.music.entity.Album"%>
<%@page import="com.music.biz.Impl.AlbumBizImpl"%>
<%@page import="com.music.biz.AlbumBiz"%>
<%@page import="com.music.Dao.Impl.AlbumDaoImpl"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html>
<head>
<title>欢迎光临 Music Store</title>
<link type="text/css" rel="Stylesheet" href="style/front.css"/>
<script type="text/javascript" src="script/jquery-1.4.1.js"></script>
</head>
<%
AlbumBiz albumItem = new AlbumBizImpl();
String id = request.getParameter("genreId"); int pageSize =3;
int pageNum =1; if(request.getParameter("page")!=null){
pageNum =Integer.valueOf(request.getParameter("page"));
} List<Album> albums = albumItem.getAlbumWithPage(Integer.valueOf(id), pageNum, pageSize); request.setAttribute("albums", albums);
request.setAttribute("pageNum", pageNum); int rows = albumItem.getRowCountWithGenreid(Integer.valueOf(id));
int pageCount = (int)Math.ceil((double)rows/pageSize);
// int pageCount =albumItem.getRowCountWithGenreid(Integer.valueOf(id));
request.setAttribute("pageCount", pageCount); request.setAttribute("genreId", id); %> <body>
<div id="wrapper">
<%@ include file="shared/front_header.jsp" %>
<div id="content">
<%@ include file="shared/front_sidebar.jsp" %>
<div id="main">
<h3 id="main-title">唱片列表</h3>
<c:forEach var="album" items="${albums}">
<table class="albumItem">
<tr>
<td rowspan="3" class="albumItem-image"><img src="CoverImages/${album.id}.jpg" alt="" /></td>
<td colspan="2" class="albumItem-title">
<a href="album.jsp?albumId=${album.id}">${album.title}</a>
</td>
</tr>
<tr>
<td class="albumItem-artist"><strong>歌手:${album.artist }</strong></td>
<td class=".albumItem-price"><strong>定价:${album.price }</strong>¥</td>
</tr>
<tr>
<td colspan="2">
${album.description}
</td>
</tr>
</table>
</c:forEach>
<hr/> <a href="album_list.jsp?page=1&genreId=${genreId}&title=${title}">第一页</a>
<c:if test="${pageNum>1 }">
<a href="album_list.jsp?page=${pageNum-1}&genreId=${genreId}&title=${title}">上一页</a> </c:if>
<c:if test="${pageNum<pageCount}">
<a href="album_list.jsp?page=${pageNum+1}&genreId=${genreId}&title=${title}">下一页</a>
</c:if>
<a href="album_list.jsp?page=${pageCount}&genreId=${genreId}&title=${title}">最后一页</a>
&nbsp;共${pageCount}页,第${pageNum}页。 </div>
<div class="clearBoth"></div>
</div>
<%@ include file="shared/front_footer.jsp" %>
</div>
</body>
</html>

对于分页,前面的博客有讲述,就不赘述了~

说实话,今天我好累了~就写那么多吧~现在距离下课还有5分钟,我要记会儿单词~

Java代码实现 增删查 + 分页——实习第四天的更多相关文章

  1. java中CRUD(增删查改)底层代码的实现

    java中CRUD(增删查改)底层代码的实现: package com.station.dao; import com.station.model.Product; import java.sql.* ...

  2. MongoDB在Java下的增删查改

    我们总不能一直使用cmd对数据库操作,数据库总是要在程序中使用的.今天来说一下怎么通过Java调用MongoDB. 学习一下最基本也是最常用的增删查改语句,这是使用数据库的基础. 注意事项: 1.要打 ...

  3. node-express项目的搭建并通过mongoose操作MongoDB实现增删改查分页排序(四)

    最近写了一个用node来操作MongoDB完成增.删.改.查.排序.分页功能的示例,并且已经放在了服务器上地址:http://39.105.32.180:3333. Mongoose是在node.js ...

  4. Java连接MySQL数据库及简单的增删查改操作

    主要摘自 https://www.cnblogs.com/town123/p/8336244.html https://www.runoob.com/java/java-mysql-connect.h ...

  5. JAVA原生mvc实现用户信息的增删查改

    笔者最近学完jsp和servlet,于是心血来潮的打算写个简单的用户案例 环境准备: 开发工具eclipse jdk-1.8.0_72 tomcat-9.0.5 前端部分: 1.自己手写了一套样式 2 ...

  6. 后端Spring Boot+前端Android交互+MySQL增删查改(Java+Kotlin实现)

    1 前言&概述 这篇文章是基于这篇文章的更新,主要是更新了一些技术栈以及开发工具的版本,还有修复了一些Bug. 本文是SpringBoot+Android+MySQL的增删查改的简单实现,用到 ...

  7. SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)

    SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)-DML 1.SQL INSERT INTO 语句(在表中插入) INSERT INTO 语句用于向表中插入新记录. SQL I ...

  8. SQL Server 表的管理_关于表的操作增删查改的操作的详解(案例代码)

    SQL Server 表的管理_关于表的操作增删查改的操作的详解(案例代码) 概述: 表由行和列组成,每个表都必须有个表名. SQL CREATE TABLE 语法 CREATE TABLE tabl ...

  9. 分享一段ios数据库代码,包括对表的创建、升级、增删查改

    分享一段ios数据库代码.包括创建.升级.增删查改. 里面的那些类不必细究,主要是数据库的代码100%可用. 数据库升级部分,使用switch,没有break,低版本一次向高版本修改. // DB.h ...

随机推荐

  1. NSA永恒之蓝病毒,如何通过360工具修复?

    简介: NSA武器库的公开被称为是网络世界"核弹危机",其中有十款影响Windows个人用户的黑客工具,包括永恒之蓝.永恒王者.永恒浪漫.永恒协作.翡翠纤维.古怪地鼠.爱斯基摩卷. ...

  2. Samba文件共享服务

    Samba起源: 早期网络想要在不同主机之间共享文件大多要用FTP协议来传输,但FTP协议仅能做到传输文件却不能直接修改对方主机的资料数据,这样确实不太方便,于是便出现了NFS开源文件共享程序:NFS ...

  3. 【JS中循环嵌套常见的六大经典例题+六大图形题,你知道哪几个?】

    首先,了解一下循环嵌套的特点:外层循环转一次,内层循环转一圈. 在上一篇随笔中详细介绍了JS中的分支结构和循环结构,我们来简单的回顾一下For循环结构: 1.for循环有三个表达式,分别为: ①定义循 ...

  4. 10、借助POI实现Java生成并打印excel报表(1)

    10.1.了解 Apache POI 实际开发中,用到最多的是把数据库中数据导出生成报表,尤其是在生产管理或者财务系统中用的非常普遍.生成报表格式一般是EXCEL或者PDF .利用Apache  PO ...

  5. 用C语言模仿Python函数

    首先得说明一点,C 语言不是函数式编程语言,要想进行完全的函数式编程,还得先写个虚拟机,然后再写个解释器才行(相当于 CPython ). 下面我们提供一个例子,说明 C 语言函数可以"适度 ...

  6. 自定义控件pickView

    package com.example.healthembed.util; import java.util.ArrayList; import java.util.List; import java ...

  7. let与const详解

    在ES6中,js首次引入了块级作用域的概念,而什么是块级作用域? 众所就知,在js当中存在预解析的概念,就是变量提升.并且只存在全局作用域和私有作用域.在全局定义的变量就是全局变量,而在函数内部定义的 ...

  8. 几个常用的linux快捷键和shell知识

    1)   !$    !$是一个特殊的环境变量,它代表了上一个命令的最后一个字符串.如:你可能会这样:     $mkdir mydir     $mv mydir yourdir     $cd y ...

  9. [.NET跨平台]Jeuxs独立版本的便利与过程中的一些坑

    本文环境与前言 之前写过一篇相关的文章:在.NET Core之前,实现.Net跨平台之Mono+CentOS+Jexus初体验 当时的部署还是比较繁琐的,而且需要联网下载各种东西..有兴趣的可以看看, ...

  10. 2016计蒜之道复赛B题:联想专卖店促销

    题解 思路: 二分答案,设我们要check的值为x. 注意到每一个礼包都有,一个U盘,一个鼠标. 剩余的,分别为一个机械键盘,一个U盘,一个鼠标. 当礼包数目为x时,我们至多可以提供a-x个普通,b- ...