gpt4 book ai didi

java - 如何在java中将proj_id的值设置为max(proj_id) + 1

转载 作者:行者123 更新时间:2023-12-01 15:52:32 25 4
gpt4 key购买 nike

我正在尝试获取 max(proj_id)+1 的值并在 vo 中设置该值,然后我将数据插入到数据库中。我正在以下代码中尝试此操作,但出现空指针异常....

public final class CreateProjDAO 
{
private static InitialContext context;
String CLASS_NAME="DBConnectionFactory";
public void submitProjectDetails(CreateProjVO createprojVO)
{
String methodname="createConnection";
Connection conn = null;
PreparedStatement psmt;
ResultSet rs=null;
Statement st=null;
int proj_id;
try {
conn = DBConnection.getJNDIConnection();

rs=st.executeQuery("select MAX(PROJ_ID) from CR_PROJECT_DETAILS");

if(!rs.next())
{
proj_id=rs.getInt(1)+1;
System.out.println("Max:"+proj_id);
}
else
{
proj_id=1;
System.out.println("Max:"+proj_id);
}

createprojVO.setId(rs.getString(proj_id));

System.out.println("in DAO");

psmt= conn.prepareStatement("insert into CR_PROJECT_DETAILS(PROJ_ID,PROJ_NAME,PROJ_COST,PROJ_MANAGER) values(?,?,?,?)");
psmt.setString(1, createprojVO.getId());
psmt.setString(2,createprojVO.getName());
psmt.setString(3,createprojVO.getCost());
psmt.setString(4,createprojVO.getManager());
psmt.executeQuery();


System.out.println("conn==="+conn);
conn.close();
System.out.println("Disconnected from database");

} catch (Exception e) {
e.printStackTrace(System.err);
System.out.println("data already exist");
System.out.println("error:"+e);

}

}
}

最佳答案

rs=st.executeQuery("select MAX(PROJ_ID) from CR_PROJECT_DETAILS");

命中语句时st尚未初始化,因此发生NPE。

关于java - 如何在java中将proj_id的值设置为max(proj_id) + 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5728017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com