从数据库

时间:2016-01-19 02:35:43

标签: java mysql

我使用MySQL来存储和检索数据,包括1张图片。

在我的Java应用程序中,用户应该插入图片,但这是可选的。因此,如果用户没有插入图片并且我尝试从我的数据库中检索所有数据,则会出现空指针异常错误。

如果从数据库中检索到的图像为空,我想显示空图像或默认图像。

这是我的代码。

//This is the data access to add and retrieve from the database.

private static Medication convertToMedication(ResultSet rs) throws SQLException {
    Medication med;

    int id=rs.getInt("idMedicationInfo");
    String diseaseName = rs.getString("diseaseName");
    String medicationName = rs.getString("medicationName");
    byte[] image = rs.getBytes("medPic");
    if (image==null){

    }
    double initialMedAmt = rs.getDouble("initialAmount");
    double servingSize = rs.getDouble("servingSize");

    boolean morning=rs.getBoolean("morning");
    boolean afternoon=rs.getBoolean("afternoon");
    boolean evening=rs.getBoolean("night");
    med=new Medication( diseaseName, medicationName,image, initialMedAmt, servingSize, morning, afternoon, evening);
    return med;
}


public static boolean createMedication(Medication med){
    // declare local variables
    boolean success = false;
    DBController db = new DBController();
    String dbQuery;
    PreparedStatement pstmt;

    // step 1 - establish connection to database
    db.getConnection();

    // step 2 - declare the SQL statement
    dbQuery = "INSERT INTO medicationinfo(diseaseName, medicationName , medPic ,initialAmount,servingSize,amountLeft,morning,afternoon,night) VALUES(?,?,?,?,?,?,?,?,?);";
    pstmt = db.getPreparedStatement(dbQuery);
    //pstmt = db.getPreparedStatement(dbQuery);

    // step 3 - to insert record using executeUpdate method
    try {


        pstmt.setBytes(3, med.getMedPic());
        pstmt.setDouble(4, med.getInitialMedAmt());
        pstmt.setDouble(5, med.getServingSize());

        pstmt.setDouble(6, med.getAmountLeft());
        //pstmt.setDouble(8, med.getmedicationServingSize());
        pstmt.setBoolean(7, true);
        pstmt.setBoolean(8, true);
        pstmt.setBoolean(9, true);
        if (pstmt.executeUpdate() == 1){
            success = true;
        }
        pstmt.close();
    } catch(Exception e){
        e.printStackTrace();
    }

    // step 4 - close connection
    db.terminate();

    return success;
}


public static ArrayList<Medication> retrieveAllMedication(){
    // declare local variables
    ArrayList<Medication> list = new ArrayList<Medication>();
    ResultSet rs;
    DBController db = new DBController();
    String dbQuery;

    // step 1 - connect to database
    db.getConnection();

    // step 2 - declare the SQL statement
    dbQuery = "SELECT * FROM medicationinfo";

    // step 3 - using DBController readRequest method
    rs = db.readRequest(dbQuery);
    try {
        while (rs.next()){
            Medication med = convertToMedication(rs);
            list.add(med);
        }   
    }catch (Exception e){
        e.printStackTrace();
    }

    // step 4 - close connection
    db.terminate();

    return list;
}

public static Medication retrieveMedicationById(int id){
    // declare local variables
    Medication med = null;
    ResultSet rs = null;
    DBController db = new DBController();
    String dbQuery;
    PreparedStatement pstmt;

    // step 1 - connect to database
    db.getConnection();

    // step 2 - declare the SQL statement
    dbQuery = "SELECT * FROM expense WHERE id = ?";
    pstmt = db.getPreparedStatement(dbQuery);

    // step 3 - execute query
    try {
        pstmt.setInt(1, id);
        rs = pstmt.executeQuery();
        if (rs.next()){ // first record found
            med = convertToMedication(rs);
        }
    } catch (Exception e){
        e.printStackTrace();
    }

    // step 4 - close connection
    db.terminate();
    return med;
}

public static ArrayList<Medication> retrieveMedicationByMorning(){
    // declare local variables
    ArrayList<Medication> list = new ArrayList<Medication>();
    ResultSet rs = null;
    DBController db = new DBController();
    String dbQuery;
    PreparedStatement pstmt;

    // step 1 - connect to database
    db.getConnection();

    // step 2 - declare the SQL statement
    dbQuery = "SELECT * FROM medicationinfo WHERE morning = ?";
    pstmt = db.getPreparedStatement(dbQuery);
    // step 3 - execute query
    try {
        while (rs.next()){
            Medication med = convertToMedication(rs);
            list.add(med);
        }   
    }catch (Exception e){
        e.printStackTrace();
    }

    // step 4 - close connection
    db.terminate();

    return list;
}


public static ArrayList<Medication> retrieveMedicationByAfternoon(){
    // declare local variables
    ArrayList<Medication> list = new ArrayList<Medication>();
    ResultSet rs = null;
    DBController db = new DBController();
    String dbQuery;
    PreparedStatement pstmt;

    // step 1 - connect to database
    db.getConnection();

    // step 2 - declare the SQL statement
    dbQuery = "SELECT * FROM medicationinfo WHERE afternoon = ?";
    pstmt = db.getPreparedStatement(dbQuery);

    // step 3 - execute query
    try {
        while (rs.next()){
            Medication med = convertToMedication(rs);
            list.add(med);
        }   
    }catch (Exception e){
        e.printStackTrace();
    }

    // step 4 - close connection
    db.terminate();

    return list;
}


public static ArrayList<Medication> retrieveMedicationByEvening(){
    // declare local variables
    ArrayList<Medication> list = new ArrayList<Medication>();
    ResultSet rs = null;
    DBController db = new DBController();
    String dbQuery;
    PreparedStatement pstmt;

    // step 1 - connect to database
    db.getConnection();

    // step 2 - declare the SQL statement
    dbQuery = "SELECT * FROM medicationinfo WHERE evening = ?";
    pstmt = db.getPreparedStatement(dbQuery);

    // step 3 - execute query
    try {
        while (rs.next()){
            Medication med = convertToMedication(rs);
            list.add(med);
        }   
    }catch (Exception e){
        e.printStackTrace();
    }

    // step 4 - close connection
    db.terminate();

    return list;
}

1 个答案:

答案 0 :(得分:1)

首先检查rs.getBytes是否为null。

byte[] image = null;
if (rs.getBytes("medPic") != null)
image = rs.getBytes("medPic");