结果集在mysql中给出null值

时间:2013-12-19 04:30:35

标签: java mysql sql jsp

这是我的代码的一部分

String qn = request.getParameter("t0");
        String o1 = request.getParameter("t1");
        String o2 = request.getParameter("t2");
        String  o3 = request.getParameter("t3");
        String  o4 = request.getParameter("t4");
        String  an = request.getParameter("a");
        String d_id=null;
        String u=request.getParameter("u");
        String s=request.getParameter("s");
        String s1=request.getParameter("s1");
        String d=request.getParameter("d");
        //InputStream inputStream = null; // input stream of the upload file

        // obtains the upload file part in this multipart request


        Connection conn = null; // connection to the database
        String message = null;  // message will be sent back to client
        try {
             DriverManager.registerDriver(new com.mysql.jdbc.Driver());
             conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
        Statement st=conn.createStatement();
        ResultSet rsta=st.executeQuery("Select id from details where us_t='"+u+"'and st='"+s+"' and std='"+s1+"' and divn='"+d+"'");   

        while(rsta.next()){
        d_id=rsta.getString("id");
         }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("ddetail id"+d_id);//Print on console
        try {
            // connects to the database

            // constructs SQL statement
          //  String sql = "INSERT INTO test4 (index_qn,q_no,q_text,as,f_name,dy,at) values (?, ?, ?, ?,?,?,?)";
            String sql = "INSERT INTO question (qn,o1,o2,o3,o4,an,d_id) values (?, ?, ?, ?, ?, ?, ?)";
            PreparedStatement statement = conn.prepareStatement(sql);

            statement.setString(1, qn);
            statement.setString(2, o1);
            statement.setString(3, o2);
            statement.setString(4, o3);
            statement.setString(5, o4);
            statement.setString(6,an;
            statement.setString(7,d_id);

            // sends the statement to the database server
            int row = statement.executeUpdate();

这里我希望d_id在我的下一个查询中传递,但是d_id给了我一个空值。我为d_id放了一个system.out,当我在控制台上检查它时它给了我一个空值。我是什么意思做错了。 当我在mysql命令行上键入相同的查询时,它工作正常。就这样。 这是一个JSP项目

1 个答案:

答案 0 :(得分:-1)

您的ResultSet可能不包含任何结果。尝试在while循环中放入一些print语句。