刚入门javaweb,自己做个jsp小练习供以后参考,本文纯jsp与后台JDBC增删改查的操作,还有一些瑕疵需要修复(当然大佬就不要挑我这个小白的毛病了,小白正在不断学习中....).
写这篇文章的目的是为了记录 怎样把数据从前台传输到后端数据库,也是对自己学习的一次小总结, 不足之处多多见谅~~~
好啦,开始整吧
(1) 数据库(mysql)只有一张表 emp
(2) eclipse建立web项目(不使用maven) 具体结构如下:
(3)上代码
后台代码 :
(3.1) 先写好连接数据库的配置文件,配置文件的作用就是方便更改我们数据库的账户密码,数据库驱动等
1 jdbc.name =root2 jdbc.password =root3 jdbc.url = jdbc:mysql://localhost:3306/cust?useSSL=false
4 jdbc.driver = com.mysql.jdbc.Driver
jdbc.properties
(3.2) util包下的工具类
1 packagecn.ssq.util;2
3 importjava.io.IOException;4 importjava.io.InputStream;5 importjava.util.Properties;6
7 public classProsutil {8
9 private staticProperties properties;10
11 static{12 properties = newProperties();13 //引入配置文件
14 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties");15
16 try{17 properties.load(is);18 } catch(IOException e) {19 //TODO Auto-generated catch block
20 e.printStackTrace();21 }22
23 }24 public staticString getStringByKey(String key){25 return properties.getProperty(key,"");26 }27 }
Prosutil.java(调用配置文件)
1 packagecn.ssq.util;2
3 importjava.sql.Connection;4 importjava.sql.DriverManager;5 importjava.sql.PreparedStatement;6 importjava.sql.ResultSet;7 importjava.sql.SQLException;8
9 public classDButil {10 //通过配置文件读取数据库的相关配置
11 private static final String URL = Prosutil.getStringByKey("jdbc.url");12 private static final String DRIVER = Prosutil.getStringByKey("jdbc.driver");13 private static final String USERNAME = Prosutil.getStringByKey("jdbc.name");14 private static final String PASSWORD = Prosutil.getStringByKey("jdbc.password");15 //建立数据库连接
16 public staticConnection getConn(){17 Connection conn = null;18
19 try{20 Class.forName(DRIVER);21 conn =DriverManager.getConnection(URL,USERNAME,PASSWORD);22 } catch(ClassNotFoundException e) {23
24 e.printStackTrace();25 } catch(SQLException e) {26
27 e.printStackTrace();28 }29 returnconn;30 }31
32 //关闭数据库连接
33 public static voidCloseAll(ResultSet rs,PreparedStatement ps, Connection conn){34
35 try{36 if(rs != null){37 rs.close();38 rs = null;39 }40 if(ps !=null){41 ps.close();42 ps = null;43 }44 if(conn!=null){45 conn.close();46 conn = null;47 }48 } catch(SQLException e) {49
50 e.printStackTrace();51 }52
53 }54
55 //private static ResultSet rs;
56 private staticPreparedStatement ps;57 private staticConnection conn;58 /**
59 * 增删改60 *@return
61 */
62 public static intexecuUpdate(String sql ,Object...param){63 int num = 0;64 conn =getConn();65 try{66 PreparedStatement ps =conn.prepareStatement(sql);67 for (int i = 0; i 74 e.printStackTrace();75 }finally{76 CloseAll(null,ps,conn);77 }78 returnnum;79 }80 /** 81 * 通用查询82 */ 83 public staticResultSet execuQuery(String sql,Object...param){84 ResultSet rs = null;85 conn =getConn();86 try{87 PreparedStatement ps =conn.prepareStatement(sql);88 for (int i = 0; i 95 e.printStackTrace();96 }97 returnrs;98 99 }100 } DButil.java(数据库工具类) (3.3)entity包下的实体类 1 packagecn.ssq.entity;2 3 public classEmp {4 5 private intid;6 privateString name;7 privateString gender;8 privateString tel;9 privateString username;10 privateString password;11 public intgetId() {12 returnid;13 }14 public void setId(intid) {15 this.id =id;16 }17 publicString getName() {18 returnname;19 }20 public voidsetName(String name) {21 this.name =name;22 }23 publicString getGender() {24 returngender;25 }26 public voidsetGender(String gender) {27 this.gender =gender;28 }29 publicString getTel() {30 returntel;31 }32 public voidsetTel(String tel) {33 this.tel =tel;34 }35 publicString getUsername() {36 returnusername;37 }38 public voidsetUsername(String username) {39 this.username =username;40 }41 publicString getPassword() {42 returnpassword;43 }44 public voidsetPassword(String password) {45 this.password =password;46 }47 publicEmp() {48 super();49 50 }51 public Emp(intid, String name, String gender, String tel, String username,52 String password) {53 super();54 this.id =id;55 this.name =name;56 this.gender =gender;57 this.tel =tel;58 this.username =username;59 this.password =password;60 }61 62 } Emp.java (3.4)dao包下的接口和接口实现类 1 packagecn.ssq.dao;2 3 importjava.util.List;4 5 importcn.ssq.entity.Emp;6 7 public interfaceEmpDao {8 9 intsave(Emp emp);10 int delete(intid);11 intupdate(Emp emp);12 ListfindAll();13 Emp getEmpById(intid);14 15 } EmpDao.java 1 packagecn.ssq.dao.impl;2 3 importjava.sql.ResultSet;4 importjava.sql.SQLException;5 importjava.util.ArrayList;6 importjava.util.List;7 8 importcn.ssq.dao.EmpDao;9 importcn.ssq.entity.Emp;10 importcn.ssq.util.DButil;11 12 public class EmpDaoImpl implementsEmpDao {13 14 Emp emp = null;15 /** 16 * 添加17 */ 18 @Override19 public intsave(Emp emp) {20 String sql = "insert into customer(id,name,gender,tel,username,password)values(?,?,?,?,?,?)";21 returnDButil.execuUpdate(sql, emp.getId(),emp.getName(),emp.getGender(),emp.getTel(),emp.getUsername(),emp.getPassword());22 }23 24 /** 25 * 删除26 */ 27 @Override28 public int delete(intid) {29 String sql = "delete from customer where id = ?";30 returnDButil.execuUpdate(sql, id);31 }32 33 /** 34 * 修改35 */ 36 @Override37 public intupdate(Emp emp) {38 String sql = "update customer set name = ?,gender=?,tel=?,username=?,password=? where id=?";39 returnDButil.execuUpdate(sql, emp.getName(),emp.getGender(),emp.getTel(),emp.getUsername(),emp.getPassword(),emp.getId());40 }41 42 /** 43 * 查询44 */ 45 @Override46 public ListfindAll() {47 List list = new ArrayList();48 String sql = "select * from customer";49 ResultSet rs =DButil.execuQuery(sql);50 try{51 while(rs.next()){52 emp = newEmp();53 emp.setId(rs.getInt("id"));54 emp.setGender(rs.getString("gender"));55 emp.setName(rs.getString("name"));56 emp.setTel(rs.getString("tel"));57 emp.setUsername(rs.getString("username"));58 emp.setPassword(rs.getString("password"));59 list.add(emp);60 }61 } catch(SQLException e) {62 63 e.printStackTrace();64 }65 66 returnlist;67 }68 69 //根据id查询 70 @Override71 public Emp getEmpById(intid) {72 Emp emp=null;73 String sql = "select * from customer where id = ?";74 ResultSet rs =DButil.execuQuery(sql, id);75 try{76 while(rs.next()){77 emp = newEmp();78 emp.setGender(rs.getString("gender"));79 emp.setName(rs.getString("name"));80 emp.setPassword(rs.getString("password"));81 emp.setUsername(rs.getString("username"));82 emp.setTel(rs.getString("tel"));83 }84 } catch(SQLException e) {85 86 e.printStackTrace();87 }88 89 90 returnemp;91 }92 93 } EmpDaoImpl.java (3.5)service包下的业务层 1 packagecn.ssq.service;2 3 importjava.util.List;4 5 importcn.ssq.entity.Emp;6 7 public interfaceServiceDao {8 9 booleansave(Emp emp);10 boolean delete(intid);11 booleanupdate(Emp emp);12 ListfindAll();13 Emp getEmpById(intid);14 } ServiceDao.java 1 packagecn.ssq.service.impl;2 3 importjava.util.List;4 5 importcn.ssq.dao.EmpDao;6 importcn.ssq.dao.impl.EmpDaoImpl;7 importcn.ssq.entity.Emp;8 importcn.ssq.service.ServiceDao;9 /** 10 * 业务调用11 *@author北城墨白12 *13 * 2018年12月13日14 */ 15 public class ServiceDaoImpl implementsServiceDao {16 17 EmpDao dao = newEmpDaoImpl();18 @Override19 public booleansave(Emp emp) {20 21 return dao.save(emp)>0;22 }23 24 @Override25 public boolean delete(intid) {26 27 return dao.delete(id)>0;28 }29 30 @Override31 public booleanupdate(Emp emp) {32 return dao.update(emp)>0;33 }34 35 @Override36 public ListfindAll() {37 returndao.findAll();38 }39 40 @Override41 public Emp getEmpById(intid) {42 43 returndao.getEmpById(id);44 }45 46 } ServiceDaoImpl.java 到这里,基本的底层代码已经结束了,剩下的就是前端jsp的编写了(记得导包,jar包的下载地址问度娘就可以了,在这我就不啰嗦了) (3.6)WebContent文件夹下的jsp页面代码 前端代码: 1 2 3 4 5 6 7 pageEncoding="UTF-8"%> 8 9 10 11 12 13 14 15 16 request.setCharacterEncoding("UTF-8");17 response.setCharacterEncoding("utf-8");18 ServiceDao service = newServiceDaoImpl();19 List list =service.findAll();20 %> 21 添加新员工 22 23 24 25 26 27 28 29 30 31 32 33 for(Emp e:list){34 %> 35 36 37 38 39 40 41 42 44 45 46 47 48 49 index.jsp(首页查询显示界面) 1 2 3 4 pageEncoding="UTF-8"%> 5 6 7 8 9 10 11 12 13 request.setCharacterEncoding("UTF-8");14 response.setCharacterEncoding("utf-8");15 String did = request.getParameter("did");16 int empid =Integer.valueOf(did);17 ServiceDao service = newServiceDaoImpl();18 19 if(service.delete(empid)==true){20 response.sendRedirect("index.jsp");21 }else{22 response.sendRedirect("index.jsp");23 }24 %> 25 26 delete.jsp(删除处理) 1 2 3 4 5 pageEncoding="UTF-8"%> 6 7 8 9 10 11 12 13 14 15 String nid = request.getParameter("nid");16 Emp e = null;17 ServiceDao service = newServiceDaoImpl();18 if(nid !=null){19 int eid =Integer.valueOf(nid);20 e =service.getEmpById(eid);21 }22 23 %> 24 25 26 27 员工编号: ">(员工编号不可重复) 28 员工姓名: "> 29 员工性别: "> 30 员工电话: "> 31 用户名:"> 32 员工密码: "> 33 34 35 36 insert_update.jsp(添加和修改共用一个显示页面) 1 2 3 4 5 pageEncoding="UTF-8"%> 6 7 8 9 10 11 12 13 14 request.setCharacterEncoding("UTF-8");15 response.setCharacterEncoding("utf-8");16 String id = request.getParameter("nid");17 String name=request.getParameter("name");18 String gender=request.getParameter("gender");19 String tel = request.getParameter("tel");20 String username=request.getParameter("username");21 String password=request.getParameter("password");22 23 Emp emp = newEmp();24 25 int eid =Integer.valueOf(id);26 27 emp.setId(eid);28 emp.setName(name);29 emp.setGender(gender);30 emp.setTel(tel);31 emp.setUsername(username);32 emp.setPassword(password);33 34 ServiceDao service = newServiceDaoImpl();35 if(service.save(emp)==true){36 response.sendRedirect("index.jsp");37 }else{38 response.sendRedirect("insert_update.jsp");39 }40 %> 41 42 doadd.jsp(添加处理) 1 2 3 4 5 pageEncoding="UTF-8"%> 6 7 8 9 10 11 12 13 14 request.setCharacterEncoding("UTF-8");15 response.setCharacterEncoding("utf-8");16 String id = request.getParameter("nid");17 String name=request.getParameter("name");18 String gender=request.getParameter("gender");19 String tel = request.getParameter("tel");20 String username=request.getParameter("username");21 String password=request.getParameter("password");22 23 Emp emp = newEmp();24 25 int eid =Integer.valueOf(id);26 27 emp.setId(eid);28 emp.setName(name);29 emp.setGender(gender);30 emp.setTel(tel);31 emp.setUsername(username);32 emp.setPassword(password);33 34 ServiceDao service = newServiceDaoImpl();35 if(service.update(emp)==true){36 response.sendRedirect("index.jsp");37 }else{38 response.sendRedirect("insert_update.jsp");39 }40 %> 41 42 doupdate.jsp(修改处理) 代码完毕! (4)运行index.jsp页面就可以正常使用了,当然这个小练习没有实用性,只是对jsp知识点的一次小回顾,效果图如下 : 结束 小白希望能和大家多多交流,互相学习,毕竟学到老活到老嘛,啊不,活到老学到老,编程(码农)就是一步步慢慢来的,一起加油吧!员工信息