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 */ }
}
}
解决方案
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.