Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7407275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:41:20+00:00 2026-05-29T05:41:20+00:00

I’m supposed to add data from my csv file into my database. I have

  • 0

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

                    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:41:21+00:00Added an answer on May 29, 2026 at 5:41 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I have a reasonable size flat file database of text documents mostly saved in
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.