java.util.Date,获取本地时间,有日期,有时间
java.sql.Timestamp,jdbc映射数据库中的dateTime或TimeStamp类型,有日期,有时间
测试案例:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Timestamp; import java.util.Date; public class testMysql { public static void main(String[] args){ testMysqlConnection(); } @SuppressWarnings("deprecation") public static void testMysqlConnection() { Connection con = null;// 创建一个数据库连接 PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement ResultSet result = null;// 创建一个结果集对象 try { Class.forName("com.mysql.jdbc.Driver");// 加载Oracle驱动程序 System.out.println("开始尝试连接数据库!"); String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8"; String user = "root";// 用户名,系统默认的账户名 String password = "123456";// 你安装时选设置的密码 long startTime = System.currentTimeMillis(); con = DriverManager.getConnection(url, user, password);// 获取连接 String maxUpdateTimesql = "select max(TOPIC_UPDATE_TIME) as maxTime from t29e0000_02aa003 "+ "where OPERATETYPE = ?"; String mimUpdateTimesql = "select min(TOPIC_UPDATE_TIME) as maxTime from t29e0000_02aa003 "+ "where OPERATETYPE = ?"; PreparedStatement ps = con.prepareStatement(maxUpdateTimesql); ps.setString(1, "U"); result = ps.executeQuery(); //result 初始游标为head,移动next到第一个记录 while(result.next()){ Timestamp maxTime=result.getTimestamp(1);//返回时间 Date tdate = new java.util.Date (maxTime.getTime()); java.sql.Date maxSQLDate = result.getDate(1);//返回日期 Date SqlDate = new java.util.Date (maxSQLDate.getTime()); Date maxDate = result.getDate(1);//返回日期 Date date = new java.util.Date (maxDate.getTime()); System.out.println("============maxTime:"+ maxTime.toLocaleString()); System.out.println("============maxSQLDate:"+ maxSQLDate.toLocaleString()); System.out.println("============SqlDate:"+ SqlDate.toLocaleString()); System.out.println("============maxDate:"+ maxDate.toLocaleString()); System.out.println("============date:"+ date.toLocaleString()); } ps = con.prepareStatement(mimUpdateTimesql); ps.setString(1, "U"); result = ps.executeQuery(); Timestamp minTime = null; while(result.next()){ minTime=result.getTimestamp(1); System.out.println("============minTime:"+ minTime.toLocaleString()); } String seSql = "select F29E0000_02AA003001,F29E0000_02AA003002,F29E0000_02AA003003,F29E0000_02AA003004,F29E0000_02AA003005 "+ " from t29e0000_02aa003 where TOPIC_UPDATE_TIME >?"; ps = con.prepareStatement(seSql); ps.setTimestamp(1, minTime); // ps.setDate(1, new java.sql.Date(56465132));//java.sql.Date 无时间 result = ps.executeQuery(); while(result.next()){ String corName = result.getString(1); System.out.println("=========corName:"+corName); System.out.println("=========CurrentRow:"+result.getRow()); } /*String iSql = "INSERT INTO user(name,age)VALUES(?,?)"; PreparedStatement ps =con.prepareStatement(iSql); ps.setString(1, "jack"); ps.setInt(2, 23); ps.execute(); ps.setString(1, "mark"); ps.setInt(2, 67); ps.execute(); String uSql = "UPDATE user SET name = ?,age=? WHERE id=?"; ps = con.prepareStatement(uSql); ps.setString(1, "donald"); ps.setInt(2, 28); ps.setInt(3, 11); ps.executeUpdate();*/ long endTime = System.currentTimeMillis(); System.out.println("============time:"+ (endTime-startTime)); System.out.println("============hashCode:"+ con.hashCode()); if(!con.isClosed()){ System.out.println("============连接成功!"); } } catch (Exception e) { System.out.println("=============连接失败:"+e.getMessage()); e.printStackTrace(); } finally { try { // 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源 // 注意关闭的顺序,最后使用的最先关闭 if (result != null) result.close(); if (pre != null) pre.close(); if (con != null) con.close(); System.out.println("数据库连接已关闭!"); } catch (Exception e) { e.printStackTrace(); } } } }
测试结果:
开始尝试连接数据库!
============maxTime:2015-1-27 9:31:07
============tdate:2015-1-27 9:31:07
============maxSQLDate:2015-1-27 0:00:00
============SqlDate:2015-1-27 0:00:00
============maxDate:2015-1-27 0:00:00
============date:2015-1-27 0:00:00
============minTime:2015-1-23 9:31:07
=========corName:佛山市好又多怡东百货商业有限公司季华分公司
=========CurrentRow:1
=========corName:佛山市好又多怡东百货商业有限公司
=========CurrentRow:2
=========corName:沃尔玛深国投百货有限公司佛山季华分店
=========CurrentRow:3
============time:213
============hashCode:17444196
============连接成功!
数据库连接已关闭!