java mysql 动态插入,使用JAVA动态插入MySql数据库

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值