前端插入date类型的数据到数据库
1 //插入 2 @Override 3 public boolean insertEmp(Emp emp) { 4 String sql = "insert into emp(lwlEmpno,lwlEname,lwlSalary,lwlHiredate,lwlSsex) " + 5 " values('" + emp.getLwlEmpno() + "','" + emp.getLwlEname() + "','" + 6 emp.getLwlSalary() + "',to_date('" + emp.getLwlHiredate() + "','yyyy-mm-dd'),'" + emp.getLwlSsex() + "')"; 7 log.debug(sql); 8 return LwlDao.exeUpdate(sql); 9 } 10 11 12 //修改 13 @Override 14 public boolean updateEmp(Emp emp, String oldEmpNo) { 15 String sql = "update emp set lwlEmpno='" + emp.getLwlEmpno() + "',lwlEname='" + emp.getLwlEname() + 16 "',lwlSalary='" + emp.getLwlSalary() + 17 "',lwlHiredate=to_date('" + emp.getLwlHiredate() + "','yyyy-mm-dd'),lwlSsex='" + emp.getLwlSsex() + "' where lwlEmpno='" + oldEmpNo + "'"; 18 log.debug(sql); 19 return LwlDao.exeUpdate(sql); 20 } 21 22 23 //获取 24 public static List<Emp> getEmp(String sql) { 25 ArrayList<Emp> lst = new ArrayList<Emp>(); 26 Connection conn = getConn(); 27 try { 28 Statement st = conn.createStatement(); 29 ResultSet rs = st.executeQuery(sql); 30 while (rs.next()) { 31 Emp emp = new Emp(); 32 emp.setLwlEmpno((rs.getString("lwlEmpno"))); 33 emp.setLwlEname(rs.getString("lwlEname")); 34 emp.setLwlSalary(Double.parseDouble(rs.getString("lwlSalary"))); 35 36 emp.setLwlHiredate(rs.getDate("lwlHiredate")+""); 37 38 emp.setLwlSsex(rs.getString("lwlSsex")); 39 lst.add(emp); 40 } 41 } catch (Exception e) { 42 System.err.println(e.getMessage()); 43 } finally { 44 if (conn != null) { 45 try { 46 conn.close(); 47 } catch (SQLException e) { 48 System.err.println(e.getMessage()); 49 } 50 } 51 } 52 return lst; 53 }
1 public class Emp { 2 private String lwlEmpno; 3 private String lwlEname; 4 private double lwlSalary; 5 private String lwlHiredate; 6 private String lwlSsex; 7 8 ……………………………… 9 }
1 create table emp( 2 lwlEmpno varchar2(20) primary key, 3 lwlEname varchar2(20), 4 lwlSalary number, 5 lwlHiredate date, 6 lwlSsex varchar2(3) 7 )
原创文章,转载请说明出处,谢谢合作