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);
}
}求大神指出錯(cuò)誤之處~
3 回答

Castle__
TA貢獻(xiàn)9條經(jīng)驗(yàn) 獲得超5個(gè)贊
哈哈找到問題了,PreparedStatement參數(shù)列表從1開始。
?pstmt.setObject(i,?obj[i]);
這個(gè)地方寫錯(cuò)了,應(yīng)該是pstmt.setObject(i+1,?obj[i]);

Castle__
TA貢獻(xiàn)9條經(jīng)驗(yàn) 獲得超5個(gè)贊
補(bǔ)充說明:emp表就是用的oracle里面scott賬戶下的測(cè)試表emp,這里沒有貼出來,大神可以用自己的oracle看一下,幫忙分析,我的代碼哪里出錯(cuò)了。

qq___524
TA貢獻(xiàn)171條經(jīng)驗(yàn) 獲得超74個(gè)贊
INSERT?INTO?EMP?(?ENAME,?JOB,?MGR,?HIREDATE,?SAL,?COMM,?DEPTNO)這里面有7列,而obj數(shù)組里面只有六個(gè)元素
添加回答
舉報(bào)
0/150
提交
取消