开发者

how to set value of proj_id as max(proj_id) + 1 in java

i am trying to obtain value of max(proj_id)+1 and set the value in vo and then i am inserting the data to db. i am trying this in the following code but i am getting null pointer exception....

public final class CreateProjDAO 
{
private sta开发者_StackOverflow中文版tic 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 hasn't been initialized when the statement is hit, thus a NPE occurs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜