I'm supposed to add data from my csv file into my database. I have no problem adding data into the database, however, I have a situation where my java program has to add new values or update current values into the database, say under my column "date". When a new data is not identified in my current database, it would be added. However, under the same row, there's a column called "demand". If the date is the same but the demand is different. It would have to be updated. At the moment, my script can only retrieve the data line by line from mysql and compare with the data collected from the csv file. When the date is different, it can add the data into a new row. The problem is that the java program that I created can only read line by line from the beginning. I would have a repetition of data because when it start comparing from the earliest data to the latest. For example, my earliest date is 01 jan and it is compared to my csv data say 31 jan. However, my later entries has a 31 jan already stored. But my java program continue to add this data in because it compared from the beginning. I will explain in greater detail if you need more information.
public void readDataBase(int val, String d1, String d2, String d3, String d4 ) throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/testdb?"
+ "user=root&password=");
statement = connect.createStatement();
resultSet = statement.executeQuery("SELECT COUNT(*) FROM testdb.amg");
while (resultSet.next()) {
total = resultSet.getInt(1);
}
resultSet = statement.executeQuery("select * from testdb.amg");
try{
int id;
String Date,Demand;
while (resultSet.next()) {
id = resultSet.getInt("id");
Date = resultSet.getString("Date");
Demand = resultSet.getString("Demand");
System.out.println("total: " + total);
System.out.println("d4: " + d4);
if (!Date.equals(d1) && !Demand.equals(d3))
{
int val1 = total +1;
preparedStatement = connect
.prepareStatement("insert into testdb.amg values (?, ? ?)");
preparedStatement.setInt(1, val1);
preparedStatement.setString(2, d1);
preparedStatement.setString(3, d3);
preparedStatement.executeUpdate();
System.out.println("UpdatedII");
}