简体   繁体   中英

How to get data from a resultset if only one of the columns is null

I am trying to set information in some txts. My code is good when all columns are full but when a column is null there is a throw exception and code just doesn't run inside the try catch.

I know that the problem is a null column because the column PhotoQ is an optional data information for the user. So a solution, maybe load a defaultPhoto in all user accounts, but will spend store in the database.

Excuse my English I'm new XD.

Example code:

try {
    PreparedStatement pstm = conn.prepareStatement(query);
    pstm.setInt(1, userid);
    pstm.setInt(2, quizzid);
    pstm.setInt(3, NumQuest);
    ResultSet rs =pstm.executeQuery();

    while (rs.next()) {
        fileBytes = rs.getBytes("PhotoQ");
        ImageIcon image = new ImageIcon(fileBytes);
        Image im = image.getImage();
        Image myImg = im.getScaledInstance(lblImage.getWidth(), lblImage.getHeight(), Image.SCALE_SMOOTH);
        ImageIcon newImage = new ImageIcon(myImg);

        if (rs.getString("Optionsid").endsWith("1")) {
            txtaA.setText(rs.getString("Options"));
            txtaQuestion.setText(rs.getString("Question"));
            if (rs.getInt("Valid")==1) {
                ckbA.setSelected(true);
            }
            else {
                ckbA.setSelected(false);
            }
        }
    }
}

It seems that your question and error are centered around null values for the PhotoQ and/or Optionsid columns. You should be using explicit checks for nulls, for each column. Only if a column be not null should you attempt to use the value.

Something along these lines should give you an idea of how you might handle this:

while (rs.next()) {
    fileBytes = rs.getBytes("PhotoQ");
    if (fileBytes != null) {            
        ImageIcon image = new ImageIcon(fileBytes);
        Image im = image.getImage();
        Image myImg = im.getScaledInstance(lblImage.getWidth(), lblImage.getHeight(), Image.SCALE_SMOOTH);
        ImageIcon newImage = new ImageIcon(myImg);
    }

    String optionsId = rs.getString("Optionsid");
    if (optionsId != null && optionsId.endsWith("1")) {
        txtaA.setText(rs.getString("Options"));
        txtaQuestion.setText(rs.getString("Question"));
        Integer valid = rs.getInt("Valid");
        if (valid != null && valid.intValue() == 1) {
            ckbA.setSelected(true);
        } else {
            ckbA.setSelected(false);
        }        
    }
}

For fix this issue in my app. I use a wasnull() but this inside a try just for ask if a record come with a null field, I dont know if a try inside a try is properly but know is working!

code:

try {
        PreparedStatement pstm = conn.prepareStatement(query1);
        pstm.setInt(1, userid);
        pstm.setInt(2, quizzid);
        pstm.setInt(3, NumQuest);
        ResultSet rs =pstm.executeQuery();
        try {
            //move result set at first row
            rs.next();
             fileBytes = rs.getBytes("PhotoQ");
            //ask if result set contain null field
            if (rs.wasNull()) {
            // this variable is defined like globlal    
            nuller=1;
            }else {
                nuller=0;
            }
        } catch (Exception f) {

        }
         //reload resulset for begin again
        rs =pstm.executeQuery();

        while (rs.next()) {
              //here is thi – Tim Biegeleisen idea
            if (nuller==0) {
                fileBytes = rs.getBytes("PhotoQ");
                ImageIcon image = new ImageIcon(fileBytes);
                Image im = image.getImage();
                Image myImg = im.getScaledInstance(lblImage.getWidth(), lblImage.getHeight(), Image.SCALE_SMOOTH);
                ImageIcon newImage = new ImageIcon(myImg);
                lblImage.setIcon(newImage);
            }

//this code only work for a query with one row result. I use while because my //teacher example is with while. whe I get finish the system will change the code

@StevenCanales,

You were on the right track you can use the wasNull() to check the value of the last column pulled from the result set. However you do not need the additional try / catch. You can do this all streamlined within the while block.

    try {
        PreparedStatement pstm = conn.prepareStatement(query1);
        pstm.setInt(1, userid);
        pstm.setInt(2, quizzid);
        pstm.setInt(3, NumQuest);
        ResultSet rs = pstm.executeQuery();

        while (rs.next()) {
            fileBytes = rs.getBytes("PhotoQ");
            // ask if result set contain null field
            if (rs.wasNull()) {
                // handle as you wish... throw an exception ?
            } else {
                ImageIcon image = new ImageIcon(fileBytes);
                Image im = image.getImage();
                Image myImg = im.getScaledInstance(lblImage.getWidth(), lblImage.getHeight(), Image.SCALE_SMOOTH);
                ImageIcon newImage = new ImageIcon(myImg);

                if (rs.getString("Optionsid").endsWith("1")) {
                    txtaA.setText(rs.getString("Options"));
                    txtaQuestion.setText(rs.getString("Question"));
                    if (rs.getInt("Valid") == 1) {
                        ckbA.setSelected(true);
                    } else {
                        ckbA.setSelected(false);
                    }
                }
            }

        }
    } catch (Exception e) {
       //handle exception
    }

The while loop will handle walking the result set appropriately. There isn't anything special you need to do to move to the first record or back or forward.

You have to check for null on fileBytes.

fileBytes = rs.getBytes("PhotoQ");  
if (fileBytes != null) {                
    ImageIcon image = new ImageIcon(fileBytes);
    Image im = image.getImage();
    Image myImg = im.getScaledInstance(lblImage.getWidth(), lblImage.getHeight(), Image.SCALE_SMOOTH);
    ImageIcon newImage = new ImageIcon(myImg);
}

Then well you have to do whatever you want with you newImage object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM