java 查询 代码_Java代码实现 增删查 + 分页——实习第四天

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

内容模块:

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

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

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.music.entity;2

3 public classGenre {4 private intid;5 privateString name;6 privateString description;7

8 public intgetId() {9 returnid;10 }11 public void setId(intid) {12 this.id =id;13 }14 publicString getName() {15 returnname;16 }17 public voidsetName(String name) {18 this.name =name;19 }20 publicString getDescription() {21 returndescription;22 }23 public voidsetDescription(String description) {24 this.description =description;25 }26

27 }

Genre.java

6fe4b4bbe3eea1b799f9400ca8b4cfc4.png

逻辑层代码展示:

GenreDao:

1 packagecom.music.Dao;2

3 importjava.util.List;4

5 importcom.music.entity.Genre;6

7 public interfaceGenreDao {8 //查询

9 public ListgetAll();10 //删除

11 public boolean deleteGenre(intid);12 //插入

13 public booleanaddGenre(Genre g);14 //更新

15 public booleanupdateGenre(Genre g);16 }

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

1 packagecom.music.Dao.Impl;2

3 importjava.sql.ResultSet;4 importjava.sql.SQLException;5 importjava.util.ArrayList;6 importjava.util.List;7

8 importcom.music.Dao.GenreDao;9 importcom.music.entity.Genre;10

11 public class GenreDaoImpl extends BaseDao implementsGenreDao{12

13 //保存获取结果

14 ArrayList genres = new ArrayList();15 @Override16 public ListgetAll() {17 try{18 //创建连接

19 openConnection();20 String sql = "select * from genre";21 //执行查询,获取结果

22 ResultSet resultSet = executeQuery(sql, null);23 //将查询结果转换成对象

24 while(resultSet.next()) {25 Genre g = newGenre();26 g.setId(resultSet.getInt("id"));27 g.setName(resultSet.getString("name"));28 g.setDescription(resultSet.getString("description"));29 genres.add(g);30 }31 } catch(ClassNotFoundException e) {32

33 e.printStackTrace();34 } catch(SQLException e) {35

36 e.printStackTrace();37 }finally{38 closeResourse();39 }40 returngenres;41 }42

43 @Override44 public boolean deleteGenre(intid) {45 boolean result = false;46 try{47 openConnection();48 String sql ="delete from genre where id = ?";49 result = excute(sql, newObject[]{id});50 } catch(ClassNotFoundException e) {51

52 e.printStackTrace();53 } catch(SQLException e) {54

55 e.printStackTrace();56 }finally{57 closeResourse();58 }59 returnresult;60 }61

62 @Override63 public booleanaddGenre(Genre g) {64 boolean result = false;65 try{66 openConnection();67 String sql ="insert into genre value(?,?,?)";68 result =excute(sql, newObject[]{69 g.getId(),70 g.getDescription(),71 g.getName()72 });73 } catch(ClassNotFoundException e) {74 e.printStackTrace();75 } catch(SQLException e) {76 e.printStackTrace();77 }finally{78 closeResourse();79 }80 returnresult;81 }82

83 @Override84 public booleanupdateGenre(Genre g) {85 boolean result = false;86 try{87 openConnection();88 String sql = "update genre set name = ?, description =? where id=?";89 result = excute(sql, newObject[]{90 g.getId()91 });92 } catch(ClassNotFoundException e) {93

94 e.printStackTrace();95 } catch(SQLException e) {96

97 e.printStackTrace();98 }finally{99 closeResourse();100 }101 returnresult;102

103 }104

105 public static voidmain(String[] args) {106 GenreDaoImpl genreDaoImpl = newGenreDaoImpl();107 genreDaoImpl.getAll();108 System.out.println(genreDaoImpl);109

110 }111

113 }

这里还必须要提出BaseDao:

1 packagecom.music.Dao.Impl;2

3 importjava.sql.Connection;4 importjava.sql.DriverManager;5 importjava.sql.PreparedStatement;6 importjava.sql.ResultSet;7 importjava.sql.SQLException;8

9 public classBaseDao {10 //连接数据库

11 private String className = "com.mysql.jdbc.Driver";12 private String dburl = "jdbc:mysql://localhost/ZJJ";13 private String user = "root";14 private String password = "root";15 privateConnection connection;16 privatePreparedStatement statement;17 privateResultSet resultSet;18

19 public void openConnection() throwsClassNotFoundException, SQLException{20 //加载驱动

21 Class.forName(className);22 //创建连接

23 connection =DriverManager.getConnection(dburl,user,password);24 }25

26 //查询方法

27 public ResultSet executeQuery(String sql,Object[] params) throwsSQLException{28 statement =connection.prepareStatement(sql);29 //追加参数

30 if(params !=null){31 int i=1;32 for(Object object : params) {33 statement.setObject(i, object);34 i++;35 }36 }37 resultSet =statement.executeQuery();38 returnresultSet;39 }40

41 //更新

42 public boolean excute(String sql,Object[] params) throwsSQLException {43 statement =connection.prepareStatement(sql);44 if(params !=null){45 int i=1;46 for(Object object : params) {47 statement.setObject(i, object);48 }49 }50 returnstatement.execute();51 }52 //释放资源

53 public voidcloseResourse(){54 try{55 if(resultSet != null){56 resultSet.close();57 }58 if(statement != null){59 statement.close();60 }61 if(connection != null){62 connection.close();63 }64

65 } catch(SQLException e) {66 //TODO Auto-generated catch block

67 e.printStackTrace();68 }69 }70

71

72

73 }

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

今天报了一个错误:

65d3c71e616fa185c2231f41c843b46d.png

错误的原因是:

8aeeb535693487f382dfa88b9e18c4c5.png

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

分页:

接口部分:

1 //分页

2 public List getAlbumWithPage(int genreid,int pageNum,int pageSize);

1 1 public List getAlbumWithPage(int genreid, int pageNum, int pageSize)

实现部分:

1 public List getAlbumWithPage(int genreid, int pageNum, intpageSize) {2 ArrayList albums = new ArrayList();3 //pageNum当前页数

4 try{5 openConnection();6 String sql= "select * from album where genreid =? limit ?,?";7 ResultSet resultSet = executeQuery(sql, newObject[]{8 genreid,9 (pageNum-1)*pageSize,10 pageSize11 });12 while(resultSet.next()) {13 Album al= newAlbum();14 al.setId(resultSet.getInt("id"));15 al.setGenreid(resultSet.getInt("genreid"));16 al.setArtist(resultSet.getString("artist"));17 al.setTitle(resultSet.getString("title"));18 al.setPrice(resultSet.getBigDecimal("price"));19 al.setStock(resultSet.getInt("stock"));20 al.setDateReleased(resultSet.getString("dateReleased"));21 al.setDescription(resultSet.getString("description"));22 albums.add(al);23 }24 } catch(ClassNotFoundException e) {25

26 e.printStackTrace();27 } catch(SQLException e) {28

29 e.printStackTrace();30 }31 return albums;

1 public List getAlbumWithPage(int genreid, int pageNum, int pageSize) {

returndaoImpl.getAlbumWithPage(genreid, pageNum, pageSize);4 }5

6 @Override7 public int getRowCountWithGenreid(intid) {8

9 returndaoImpl.getAlbumWithGenreid(id).size();10 }

JSP代码部分

1

2

3

4

5

6

7

8

9

10

11

欢迎光临 Music Store

12

13

14

15

16 AlbumBiz albumItem = newAlbumBizImpl();17 String id = request.getParameter("genreId");18

19 int pageSize =3;20 int pageNum =1;21

22 if(request.getParameter("page")!=null){23 pageNum =Integer.valueOf(request.getParameter("page"));24 }25

26 List albums =albumItem.getAlbumWithPage(Integer.valueOf(id), pageNum, pageSize);27

28 request.setAttribute("albums", albums);29 request.setAttribute("pageNum", pageNum);30

31 int rows =albumItem.getRowCountWithGenreid(Integer.valueOf(id));32 int pageCount = (int)Math.ceil((double)rows/pageSize);33 //int pageCount =albumItem.getRowCountWithGenreid(Integer.valueOf(id));

34 request.setAttribute("pageCount", pageCount);35

36 request.setAttribute("genreId", id);37

38

39 %>

40

41

42

43

44

45

46

47

唱片列表

48

49

50

51

52

53 ${album.title}

54

55

56

57

歌手:${album.artist }

58

定价:${album.price }

59

60

61

62 ${album.description}63

64

65

66

67


68

69 第一页

70

71 上一页

72

73

74

75 下一页

76

77 最后一页

78  共${pageCount}页,第${pageNum}页。79

80

81

82

83

84

85

86

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值