I've done this code:
try{
String insertSQL = "INSERT INTO visit(Doc_Number, Pat_Number, Visit_Date, Price) VALUES (?,?,?,?)";
PreparedStatement updateStatement = conn.prepareStatement(insertSQL);
Statement st = conn.createStatement(
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
for (List<String> row : fileContents){
ResultSet rs = st.executeQuery("SELECT Doc_Number, Pat_Number, Visit_Date, Price FROM visit");
int i =0;
while(rs.next()){
String[] rowArray = (String[]) row.toArray(new String[0]);
if(rs.getInt(1) == this.getIntegerFromStringOrNull(getValueIfNotNull(rowArray, 0)) && rs.getInt(2)== this.getIntegerFromStringOrNull(getValueIfNotNull(rowArray, 1))
&& rs.getDate(3)==this.getDateFromString(getValueIfNotNull(rowArray, 2))){
rs.updateFloat(4, this.getFloatFromStringOrNull(getValueIfNotNull(rowArray, 3)));
System.out.println("inside if");
}else{
updateStatement.clearParameters();
this.setValueOrNull(updateStatement, 1, this.getIntegerFromStringOrNull(getValueIfNotNull(rowArray, 0)));
this.setValueOrNull(updateStatement, 2, this.getIntegerFromStringOrNull(getValueIfNotNull(rowArray, 1)));
this.setValueOrNull(updateStatement, 3, this.getDateFromString(getValueIfNotNull(rowArray, 2)));
this.setValueOrNull(updateStatement, 4, this.getFloatFromStringOrNull(getValueIfNotNull(rowArray, 3)));
updateStatement.executeUpdate();
}
}
}
}catch (SQLException e){
System.out.println("message: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
}
}
I'm sure that I read correctly from a file and I'm also connected correctly to the database. Now I'm trying to insert the rows from file into the table but before I need to check that if some row already exists in table. In this case, I only need to update the last parameter (Price) of the row.
Table implementation:
CREATE TABLE VISIT
(
Doc_Number INTEGER,
Pat_Number INTEGER,
Visit_Date DATE,
Price DECIMAL(7,2),
CONSTRAINT Visit_pk PRIMARY KEY (Doc_Number, Pat_Number, Visit_Date),
CONSTRAINT ck_Price CHECK (Price >0),
CONSTRAINT Visit_Doctor_fk FOREIGN KEY (Doc_Number) REFERENCES DOCTOR(Doc_Number),
CONSTRAINT Visit_PATIENT_fk FOREIGN KEY (Pat_Number) REFERENCES PATIENT(Pat_Number)
);
Maybe you need or not to know the implementation of setValueOrNull
or getIntegerFromStringOrNull
.... so I'll post their implemetation, sorry, is a bit long:
private void setUpdateParameters(PreparedStatement updateStatement,
List<String> row) throws SQLException {
String[] rowArray = (String[]) row.toArray(new String[0]);
setValueOrNull(updateStatement, 2, getIntegerFromStringOrNull(getValueIfNotNull(rowArray, 0))); // Doc_Number
setValueOrNull(updateStatement, 3, getIntegerFromStringOrNull(getValueIfNotNull(rowArray, 1))); // Pac_Number
setValueOrNull(updateStatement, 4, getDateFromString(getValueIfNotNull(rowArray, 2))); // Visit_Date
setValueOrNull(updateStatement, 1, getFloatFromStringOrNull(getValueIfNotNull(rowArray, 3))); // Price
}
private void setInsertParameters(PreparedStatement insertStatement,
List<String> row) throws SQLException {
String[] rowArray = (String[]) row.toArray(new String[0]);
setValueOrNull(insertStatement, 1, getIntegerFromStringOrNull(getValueIfNotNull(rowArray, 0))); // Doc_Number
setValueOrNull(insertStatement, 2, getIntegerFromStringOrNull(getValueIfNotNull(rowArray, 0))); // Pac_Number
setValueOrNull(insertStatement, 3, getDateFromString(getValueIfNotNull(rowArray, 2))); // Visit_Date
setValueOrNull(insertStatement, 4, getFloatFromStringOrNull(getValueIfNotNull(rowArray, 3))); // Price
}
private Integer getIntegerFromStringOrNull(String integer)
{
return (null != integer) ? Integer.valueOf(integer) : null;
}
private Float getFloatFromStringOrNull(String Sfloat)
{
return (null != Sfloat) ? Float.valueOf(Sfloat) : null;
}
private String getValueIfNotNull(String[] rowArray, int index) {
String ret = null;
if (index < rowArray.length) {
ret = rowArray[index];
}
return ret;
}
private void setValueOrNull(PreparedStatement preparedStatement,
int parameterIndex, Integer value) throws SQLException {
if (value == null) {
preparedStatement.setNull(parameterIndex, Types.INTEGER);
} else {
preparedStatement.setInt(parameterIndex, value);
}
}
private void setValueOrNull(PreparedStatement preparedStatement,
int parameterIndex, Float value) throws SQLException {
if (value == null ) {
preparedStatement.setNull(parameterIndex, Types.VARCHAR);
} else {
preparedStatement.setFloat(parameterIndex, value);
}
}
private void setValueOrNull(PreparedStatement preparedStatement,
int parameterIndex, java.util.Date value) throws SQLException {
if (value == null) {
preparedStatement.setNull(parameterIndex, Types.TIMESTAMP);
} else {
java.sql.Timestamp timestamp = new java.sql.Timestamp(
value.getTime());
preparedStatement.setTimestamp(parameterIndex, timestamp);
}
}
private Date getDateFromString(String date)
{
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
try {
return (Date)formatter.parse(date);
} catch (ParseException e) {
System.err.println("Error parsing date " + date + ". Returning null");
return null;
}
}
}
Well, the code seems not enter into the if
, because the first row already exists in table, when I execute program I get the information that the content of first row already exists. What I'm doing wrong? I supose that I've done wrong the if clause but I can't see the error or maybe that's not the correct way to compare rows from file between table to do a update or insert in correct case.
PS: Also, if I try to erase the rows that I know that primary key already exists in table, the fucntion only add 1 new row to the table and next it says that that row already exists instead of add next line.
EDIT
I changed rs.getDate(3)
for rs.getTimestamp(3)
, if I do
System.out.println(rs.getTimestamp(3) +" " + this.getDateFromString(getValueIfNotNull(rowArray, 2)));
I get this exit:
2011-03-30 00:00:00.0 Wed Mar 30 00:00:00 CEST 2011
2010-03-14 00:00:00.0 Wed Mar 30 00:00:00 CEST 2011
2011-02-15 00:00:00.0 Wed Mar 30 00:00:00 CEST 2011
2011-06-07 00:00:00.0 Wed Mar 30 00:00:00 CEST 2011
2014-12-30 00:00:00.0 Wed Mar 30 00:00:00 CEST 2011
As I can see, when I do this.getDateFromString(getValueIfNotNull(rowArray, 2))
program prints the same date 170 times which is the number of entries in my table, next prints the next date read from file 170 again, etc, so, there is still some error, I think, about the date format coparising, anyone knows what happens?