今天项目内容已经开始了,并且已经完成好多基本操作,今天就开始总结今天学习到的内容,和我遇到的问题,以及分析这其中的原因。
内容模块:
1:Java代码实现对数据库的增删查;
2:分页且获取页面信息;
这里针对于项目里面的Genre实体,以及对于它的操作进行举例
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
逻辑层代码展示:
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 }
这个里面的方法都会获得调用。
今天报了一个错误:
错误的原因是:
这个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 Store12
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
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分钟,我要记会儿单词~