import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.PreparedStatement;
import
java.sql.ResultSet;
import
java.sql.Statement;
import
java.text.SimpleDateFormat;
public
class
GettingTime
{
public
static
void
main(String[] args) throws
Exception {
Connection conn =
getConnection();
Statement st =
conn
.createStatement();
st.executeUpdate("create
table gettingtime (id int,myDate TIMESTAMP );");
String
INSERT_RECORD = "insert into
gettingtime(id, myDate) values(?, ?)";
PreparedStatement
pstmt = conn.prepareStatement(INSERT_RECORD);
pstmt.setString(1,
"1");
java.sql.Timestamp
sqlDate = new
java.sql.Timestamp(new
java.util.Date().getTime());
pstmt.setTimestamp(2,
sqlDate);
pstmt.executeUpdate();
ResultSet rs =
st.executeQuery("SELECT * FROM
gettingtime");
while
(rs.next()) {
java.sql.Timestamp
sd=rs.getTimestamp(2);
System.out.println(sd);
String S = new
SimpleDateFormat("dd-MMM-yyyy
hh:mm:ss").format(sd);
System.out.println(S);
}
rs.close();
st.close();
conn.close();
}
private
static
Connection getConnection() throws
Exception {
Class.forName("com.mysql.jdbc.Driver");
String
url = "jdbc:mysql://localhost:3306/reg";
return
DriverManager.getConnection(url,
"root",
"root");
}
}
No comments:
Post a Comment