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);
}
}求大神指出错误之处~
3 回答
Castle__
TA贡献9条经验 获得超5个赞
哈哈找到问题了,PreparedStatement参数列表从1开始。
pstmt.setObject(i, obj[i]);
这个地方写错了,应该是pstmt.setObject(i+1, obj[i]);
Castle__
TA贡献9条经验 获得超5个赞
补充说明:emp表就是用的oracle里面scott账户下的测试表emp,这里没有贴出来,大神可以用自己的oracle看一下,帮忙分析,我的代码哪里出错了。
qq___524
TA贡献171条经验 获得超74个赞
INSERT INTO EMP ( ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)这里面有7列,而obj数组里面只有六个元素
添加回答
举报
0/150
提交
取消