I am using MySQL to store and retrieve data, including 1 picture.
In my Java application, Users are supposed to insert a picture, but this is optional. So if a user does not insert a picture and I try to retrieve all the data from my database, there is a null pointer exception error.
I want to display an empty image or a default image if the image retrieved from the database is null.
This is my code.
//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;
}