一、示例代码
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import com.broadtech.bdvmonitor.utils.JdbcUtils_C3P0;
public class TestSqlDateAndUtilDate {
// @Test
public static void c3p0DataSourceTest() {
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
Long lo=1530756478401L;
try{
//获取数据库连接
conn = JdbcUtils_C3P0.getConnection();
/*
* CREATE TABLE ml_datetest(
starttime DATETIME,
endtime DATE,
thirTime TIMESTAMP,
fourTime DATETIME
)
*/
String v_Sql1="insert into ml_datetest(starttime,endtime,thirTime,fourTime) values(?,?,?,?)";
String v_Sql2="select * from ml_datetest ";
String sql =v_Sql1;
st = conn.prepareStatement(sql);
st.setDate(1, new java.sql.Date(lo));
st.setDate(2, new java.sql.Date(lo));
st.setDate(3, new java.sql.Date(lo));
st.setTimestamp(4,new Timestamp(lo));
st.addBatch();
st.executeBatch();
st = conn.prepareStatement(v_Sql2);
rs= st.executeQuery();
if(rs.next()){
System.out.println( rs.getString(1)+" | "
+ rs.getString(2)+" | "
+ rs.getString(3)+" | "
+ rs.getString(4)
);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
//释放资源
JdbcUtils_C3P0.close(conn, st, rs);
}
}
public static void main(String[] args) {
System.out.println("测试开始");
c3p0DataSourceTest();
System.out.println("测试结束");
}
}
二、数据库示例