Eugene
Eugene

Reputation: 263

Adding data into mysql database without repeating data

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");

                    }

Upvotes: 2

Views: 1866

Answers (2)

prajeesh kumar
prajeesh kumar

Reputation: 1966

As Rocky said, define the unique key on the table. Since the database is MySQL, you can achieve what you want by simple query with INSERT... ON DUPLICATE KEY UPDATE ... INSERT ... ON DUPLICATE KEY UPDATE Syntax

In your case it can be

insert into  testdb.amg values (?, ? ?) on duplicate key update <update fields here>

So the final query shall be

"insert into testdb.amg values (?, ?, ?) on duplicate key update Demand=?"

and add

preparedStatement.setString(4, d3);

Upvotes: 1

Rocky
Rocky

Reputation: 951

Identify the distinct columns in your database (date + demand + whatever_is_distinct ) and define a UNIQUE constraint on that combination.

By doing that ,during insertion, if there is any Constraint Violation exception that gets thrown , update that record or else the record gets inserted.

Upvotes: 3

Related Questions