package JDBC;
import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/** *//**
*
* @author oakertree
*
*/
public class TestTransaction {
public static void main(String[] args) {
Connection con = null;
Statement stmt = null;
// PreparedStatement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
// new com.mysql.jdbc.Driver();
con = DriverManager.getConnection("jdbc:mysql://localhost:3307/test", "root", "admini");
con.setAutoCommit(false);
stmt = con.createStatement();
stmt.addBatch("INSERT INTO time VALUES ( NOW(), NOW(), NOW())");
stmt.addBatch("INSERT INTO time VALUES ( NOW(), NOW(), NOW())");
stmt.addBatch("INSERT INTO time VALUES ( NOW(), NOW(), NOW())");
stmt.addBatch("INSERT INTO time VALUES ( NOW(), NOW(), NOW())");
stmt.addBatch("INSERT INTO time VALUES ( NOW(), NOW(), NOW())");
stmt.addBatch("INSERT INTO time VALUES ( NOW(), NOW(), NOW())");
stmt.executeBatch();
con.commit();
con.setAutoCommit(true);
stmt.close();
/**//*
System.out.println(Date.valueOf("2004-02-19"));
System.out.println(Time.valueOf("14:29:32"));
System.out.println(Timestamp.valueOf("2008-12-01 12:25:32"));
stmt = con.prepareStatement("INSERT INTO time VALUES (?, ?, ?)");
stmt.setDate(1, Date.valueOf("2004-12-02"));
stmt.setTime(2, Time.valueOf("14:02:32"));
stmt.setTimestamp(3, Timestamp.valueOf("2004-12-01 12:25:32"));
stmt.addBatch();
stmt.setDate(1, Date.valueOf("2005-12-02"));
stmt.setTime(2, Time.valueOf("15:02:32"));
stmt.setTimestamp(3, Timestamp.valueOf("2005-12-01 12:25:32"));
stmt.addBatch();
stmt.setDate(1, Date.valueOf("2006-12-02"));
stmt.setTime(2, Time.valueOf("16:02:32"));
stmt.setTimestamp(3, Timestamp.valueOf("2006-12-01 12:25:32"));
stmt.addBatch();
stmt.setDate(1, Date.valueOf("2007-12-02"));
stmt.setTime(2, Time.valueOf("17:02:32"));
stmt.setTimestamp(3, Timestamp.valueOf("2007-12-01 12:25:32"));
stmt.addBatch();
stmt.executeBatch();
stmt.close();
*/
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (BatchUpdateException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
try {
if(con != null) {
con.rollback();
con.setAutoCommit(true);
}
} catch(SQLException ie) {
ie.printStackTrace();
}
} finally {
try {
if(stmt != null) {
stmt.close();
stmt = null;
}
if(con != null) {
con.close();
con = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/**//*
+------------+----------+---------------------+
| date | time | datetime |
+------------+----------+---------------------+
| 2007-10-16 | 01:01:17 | 2007-10-16 01:01:17 |
| 2007-10-17 | 01:02:22 | 2006-05-12 00:00:00 |
| 2007-12-19 | 00:13:04 | 2007-12-19 00:13:04 |
| 2007-12-22 | 14:13:48 | 2007-12-22 14:13:48 |
| 2007-12-22 | 14:14:52 | 2007-12-22 14:14:52 |
| 2007-12-22 | 14:14:52 | 2007-12-22 14:14:52 |
| 2007-12-22 | 14:14:52 | 2007-12-22 14:14:52 |
| 2007-12-22 | 14:14:52 | 2007-12-22 14:14:52 |
| 2007-12-22 | 14:14:52 | 2007-12-22 14:14:52 |
| 2007-12-22 | 14:14:52 | 2007-12-22 14:14:52 |
| 2004-12-02 | 14:02:32 | 2004-12-01 12:25:32 |
| 2005-12-02 | 15:02:32 | 2005-12-01 12:25:32 |
| 2006-12-02 | 16:02:32 | 2006-12-01 12:25:32 |
| 2007-12-02 | 17:02:32 | 2007-12-01 12:25:32 |
+------------+----------+---------------------+
*/
posted on 2007-12-22 16:13
凌晨风 阅读(807)
评论(0) 编辑 收藏 所属分类:
Java学习笔记