java date类_Java的败笔-Date类
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
* 日期增改查測試
*
* @author leizhimin 2009-11-9 11:15:46
*/
public class Test {
private static final String data_format = "yyyy-MM-dd kk:mm:ss";
private static final SimpleDateFormat sf = new SimpleDateFormat(data_format);
public static void main(String[] args) throws SQLException, ParseException {
Timestamp ts = new Timestamp(sf.parse("2009-12-10 13:33:15").getTime());
System.out.println(ts);
//????????????????test_insert1();
//????????????????test_insert2();
//????????????????test_insert3();
//????????????????test_update1();
//????????????????test_update2();
//????????????????test_query2();
}
public static void test_insert1() throws SQLException {
Connection conn = ConnTools.makeConnection();
Statement stmt = conn.createStatement();
String sql_1 = "insert into testdate(code,crdate) values ('x',CURRENT_TIMESTAMP)";
String sql_2 = "insert????into testdate(code,crdate) values ('y','2009-11-09 13:00:23')";
stmt.executeUpdate(sql_1);
stmt.executeUpdate(sql_2);
}
public static void test_insert2() throws SQLException {
Connection conn = ConnTools.makeConnection();
String sql = "insert into testdate(code,crdate) values ('z',?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
pstmt.executeUpdate();
}
public static void test_insert3() throws SQLException {
Connection conn = ConnTools.makeConnection();
String sql = "insert into testdate(code,crdate) values ('w',?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "2009-11-09 13:00:23");
pstmt.executeUpdate();
}
public static void test_update1() throws SQLException {
Connection conn = ConnTools.makeConnection();
Statement stmt = conn.createStatement();
String sql = "update testdate set crdate = '2009-12-12 13:33:15' where code = 'z'";
stmt.executeUpdate(sql);
}
public static void test_update2() throws SQLException {
Connection conn = ConnTools.makeConnection();
String sql = "update testdate set crdate = ? where code = 'z'";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "2009-12-12 13:33:15");
pstmt.executeUpdate(sql);
}
//????????public static void test_update3() throws SQLException, ParseException {
//????????????????Connection conn = ConnTools.makeConnection();
//????????????????String sql = "update testdate set crdate = ? where code = 'z'";
//????????????????PreparedStatement pstmt = conn.prepareStatement(sql);
//????????????????pstmt.setTimestamp(1, new Timestamp(sf.parse("2009-11-09 13:00:23").getTime()));
//????????????????pstmt.executeUpdate(sql);
//????????}
public static void test_query1() throws SQLException {
Connection conn = ConnTools.makeConnection();
String sql = "select * from testdate where crdate > '2009-11-09 13:00:23' and crdate < '2009-11-10 13:00:23'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(">>> id=" + rs.getLong("id") + "; code=" + rs.getString("code") + "; crdate=" + rs.getTimestamp("crdate"));
}
conn.close();
}
//????????public static void test_query2() throws SQLException, ParseException {
//????????????????Connection conn = ConnTools.makeConnection();
//????????????????String sql = "select * from testdate where crdate > ? and crdate < ?";
//????????????????PreparedStatement pstmt = conn.prepareStatement(sql);
????????????????pstmt.setString(1,"2009-11-09 13:00:23");
????????????????pstmt.setString(2,"2009-11-10 13:00:23");
//????????????????pstmt.setTimestamp(1,new Timestamp(sf.parse("2009-11-09 13:00:23").getTime()));
//????????????????pstmt.setTimestamp(2,new Timestamp(sf.parse("2009-11-10 13:00:23").getTime()));
//????????????????ResultSet rs = pstmt.executeQuery(sql);
//????????????????while (rs.next()) {
//????????????????????????System.out.println(">>> id=" + rs.getLong("id") + "; code=" + rs.getString("code") + "; crdate=" + rs.getTimestamp("crdate"));
//????????????????}
//????????????????conn.close();
//????????}
}
總結(jié)
以上是生活随笔為你收集整理的java date类_Java的败笔-Date类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 回来吧回来吧是哪首歌啊?
 - 下一篇: 论文怎么写啊..求各位大仙知道学渣啊跪求