import dao.EMPDao; import model.Emp; public class EmpActive { EMPDao empDao = new EMPDao(); public void addEmp(Emp emp){ String sql = " INSERT INTO EMP ( ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES ( ?,?,?,TO_DATE('13-6月 -16', 'DD-MON-RR'),?,?,? )"; Object [] obj = {emp.getEname(),emp.getJob(),emp.getMgr(),emp.getSal(),emp.getComm(),emp.getDepino()}; empDao.addUpDel(sql, obj); } }
import java.sql.SQLException; public class EMPDao extends BaseDao { //增删改通用方法 public void addUpDel(String sql , Object[] obj){ conn = getConnection(); try { pstmt=conn.prepareStatement(sql); if(null!=obj || 0!=obj.length){ for (int i = 0; i < obj.length; i++) { pstmt.setObject(i, obj[i]); } } pstmt.execute(); } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(); } } }
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import util.PropertiesUtil; public class BaseDao { private final static String DRIVER=PropertiesUtil.getValue("DRIVER"); private final static String URL=PropertiesUtil.getValue("URL"); private final static String USERNAME=PropertiesUtil.getValue("USERNAME"); private final static String PASSWORD=PropertiesUtil.getValue("PASSWORD"); protected Connection conn = null ; protected PreparedStatement pstmt = null; protected ResultSet resultSet = null ; protected Connection getConnection(){ try { Class.forName(DRIVER); conn= DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } return conn; } protected void closeAll(){ if(resultSet!=null) try { resultSet.close(); if(pstmt!=null) pstmt.close(); if(conn!=null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } // public static void main(String[] args) { // BaseDao bd = new BaseDao(); // System.out.println(bd.getConnection()); // bd.closeAll(); // } }
import java.util.Date; import control.EmpActive; import model.Emp; public class View { public static void main(String[] args) { EmpActive ea = new EmpActive(); Emp emp = new Emp(7566, "JULLY", "SALE", 7698 , new Date() , 1600.00, 300.00, 30); ea.addEmp(emp); } }
求大神指出错误之处~
Castle__
Castle__
qq___524