JAva JDBC-挿入時間タイプ

1503 ワード

public class Demo7 {
    public static void main(String[] args) {
        Connection conn=null;
        PreparedStatement ps=null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","dyl123");

        ps=conn.prepareStatement("insert into t_user(username,pwd,regTime,lastLoginTime) values(?,?,?,?)");
        ps.setObject(1, "html5");
        ps.setObject(2, "12355");

        //java.sql.Date,     
        //java.sql.Time,     
        //java.sql.Timestamp,        

        //sql.Date()      ,      lang     
        java.sql.Date date=new java.sql.Date(System.currentTimeMillis());
        ps.setDate(3, date);
        //   ,    lang              
        java.sql.Timestamp stamp=new java.sql.Timestamp(System.currentTimeMillis());
        ps.setTimestamp(4,stamp);

        //      

        ps.execute();

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }finally {

        try {
            if(null!=ps)
            {
                ps.close();
            }
        }catch(SQLException e)
        {
            e.printStackTrace();
        }
        try {
            if(null!=conn)
            {
                conn.close();
            }
        }catch(SQLException e)
        {
            e.printStackTrace();
        }
    }

}
}