数据库(sql)截取日期

案例:2011-03-18 15:38:11.0

截取语句 1 :
<%
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","zhang","zhang");
stmt=conn.createStatement();
String url="select * from (select t.*,to_char(t.time,'yyyy:mm:dd hh:mm:ss')time1 from you t order by t.time desc)";
rs=stmt.executeQuery(url);
while(rs.next()){
// out.print(rs.getString("name"));
// out.print("<br/>");
out.print(rs.getString("time1"));
out.print("<br/>");
out.print(rs.getString("discuss"));
out.print("<br/>");
}
}catch(Exception e){
e.printStackTrace();
}


%>


截取结果:
2011:03:19 10:03:59





截取语句 2 :

<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "zhang",
"zhang");
stmt = conn.createStatement();
String url = "select * from (select * from you order by time desc) where rownum<=5";
rs = stmt.executeQuery(url);
while (rs.next()) {
//out.print(rs.getString("name"));
SimpleDateFormat sd=new SimpleDateFormat("yyyy:MM:dd hh:mm:ss");
String showtime=sd.format(rs.getTimestamp("time"));
out.print(showtime);
out.print("<br/>");
out.print(rs.getString("discuss"));
out.print("<br/>");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
%>

截取结果:
2011:03:19 10:03:59

你可能感兴趣的:(oracle,sql,jdbc)