1

I am trying to insert into mysql database dynamically. But I always see only current record in the database.. It is not appending into the particular column one after one another.. it just replace the previous entry made... I have to do some sort of commit after each data is added into the database? I think it performs auto commit automatically??

if (m.find() && thirdentry.startsWith("LO")) {
    Connection conn = null;
        Statement s = null;
        PreparedStatement ps = null;

        try
        {

        conn = DriverManager.getConnection
        ("jdbc:mysql://localhost/?user=root&password=admin"); 
        s=conn.createStatement();

        s.executeUpdate("CREATE DATABASE IF NOT EXISTS crawler");
        s.executeUpdate("USE crawler");

        s.executeUpdate ("DROP TABLE IF EXISTS crawl");

        s.executeUpdate (
        "CREATE TABLE crawl ("
        + "id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
        + "PRIMARY KEY (id),"
        + "url VARCHAR(125), timestamp DATETIME, response TEXT, chksum TEXT)");


        java.util.Date date= new java.util.Date();

    //I always see only the current record.. not the full record
        ps = conn.prepareStatement (
        "INSERT INTO crawl (url, timestamp, response, chksum) VALUES(?,?,?,?)");
        ps.setString (1, url1.toString());
        ps.setString (2, new Timestamp(date.getTime()).toString());
        ps.setString (3, status);
        ps.setString (4, hash);
        int count = ps.executeUpdate ();
        s.close();
        ps.close ();

        System.out.println (count + " rows were inserted");
    }
 catch (Exception e)
   {
      System.err.println ("Cannot connect to database server" +e.getMessage());
     }
       finally
       {
     if (conn != null)
            {
                try
           {
               conn.close ();
                System.out.println ("Database connection terminated");
                                                        }
                   catch (Exception e) { /* ignore close errors */ }
                                                    }
                                                }
3
  • 2
    Hm? ` s.executeUpdate ("DROP TABLE IF EXISTS crawl");` would remove your table always and you will see only newest version of 'crawl' table at the end of this execution ?!?? Commented Sep 7, 2011 at 21:13
  • You are dropping and creating a new table every time this function is called. How can it have the previous values.? Commented Sep 7, 2011 at 21:15
  • @all... my bad... I was testing somehow by dropping the table... And when I started running in real.. I forgot to remove the DROP table part... Commented Sep 7, 2011 at 21:23

1 Answer 1

5

You're dropping the table and re-creating it every time you run the app. Create it once outside of the app, and let the app update it.

Sign up to request clarification or add additional context in comments.

1 Comment

,my bad... I was testing somehow by dropping the table... And when I started running in real.. I forgot to remove the DROP table part...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.