java sql date转换_java.util.Date与java.sql.Date的相互转换

本文介绍了一个使用Java进行数据库插入操作的方法。具体实现了通过输入数据库驱动、URL、用户名、密码等参数,利用PreparedStatement预编译语句的方式插入数据,并对可能发生的异常进行了捕获和输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

private void insertData(final String driver, final String url,

final String userName, final String password, int psize,

String fileName, HashMap map,HttpServletResponse response) throws IOException {

PrintWriter out=response.getWriter();

try {

try {

Class.forName(driver).newInstance();

} catch (InstantiationException e) {

// TODO Auto-generated catch block

out.println(e.getMessage());

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

out.println(e.getMessage());

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

out.println(e.getMessage());

}

Connection conn = DriverManager.getConnection(url, userName,

password);

String sql = "insert into  frame_res_photo(resuri,restype, filename,photo,psize,phototype,photoext,sortno,description,creatorid,createdate) values(?,?,?,?,?,?,?,?,?,?,?)";

PreparedStatement pstmt = conn.prepareStatement(sql);

pstmt.setString(1, map.get("resuri"));   pstmt.setString(2, "FRT001");   pstmt.setString(3, fileName);   pstmt.setString(4, map.get("photo"));   pstmt.setBigDecimal(5, new BigDecimal(psize));   pstmt.setString(6, "FPT004");   pstmt.setString(7, "png");   pstmt.setBigDecimal(8, new BigDecimal(0));   pstmt.setString(9, map.get("description"));   pstmt.setString(10, "阮国辉");   /**java.util.Date与java.sql.Date的相互转换**/   java.util.Date now = new java.util.Date();   DateFormat df = new SimpleDateFormat("yyyy-MM-dd");   Date sd = Date.valueOf(df.format(now));   /**************************************/   pstmt.setDate(11, sd);   int num = pstmt.executeUpdate();            out.println(num);  } catch (SQLException e) {   // TODO: handle exception   out.println(e.getMessage());  } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值