81,094
社区成员
发帖
与我相关
我的任务
分享
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class SaleDao {
private static final String FIND_BY_ID = "select * from sales_tab where id = ?";
private static final String FIND_ALL = "select * from sales_tab";
private static final String SAVEM = "insert into sales_tab (year_id,month_id,day_id,sales_value) values(?,?,?,?)";
public Sale findById(int id){}
public List<Sale> findAll(){}
public boolean save(Sale sale){
try{
Connection conn = DBUtilm.getConnection();
/**
* 为什么当我用DBUtilo连接oracle(v11.2.0)时,下面这行会出错?
* PreparedStatement ps = conn.prepareStatement(SAVEM,new String[]{"id"});
* 然而当我用DBUtilm连接mysql时,程序正常走完?
* 是oracle版本问题吗,我看别人oracle 10g 可以运行的
* 代码应该没有问题
*/
PreparedStatement ps = conn.prepareStatement(SAVEM,new String[]{"id"});
ps.setInt(1,sale.getYear_id());
ps.setInt(2, sale.getMonth_id());
ps.setInt(3, sale.getDay_id());
ps.setInt(4, sale.getSales_value());
if(ps.executeUpdate()>0){
ResultSet rs = ps.getGeneratedKeys();
rs.next();
int id = rs.getInt(1);
sale.setId(id);
ps.close();
return true;
}
ps.close();
}catch(Exception e){
e.getStackTrace();
}finally{
DBUtilo.closeConnection();
}
return false;
}