PreparedStatement设置日期型参数

本文详细介绍了使用PreparedStatement进行数据库更新操作的过程,包括设置自动提交为false,构建SQL更新语句,以及如何正确设置参数类型。此外,文章还展示了如何通过Connection的commit()方法提交事务,确保数据的一致性和完整性。

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

PreparedStatement upStatement = null;
Connection connection = null;

connection = getConnection(sclMiddleTble);
connection.setAutoCommit(false);

String updateMess = "UPDATE tableName" 
	" SET STATUS = ?, EXCEPTION_MESS = ?,BACK_TIME=?,BACK_DATE=? WHERE ID = ?";
upStatement = connection.prepareStatement(updateMess);
//构建一个java.sql.Date对象
java.sql.Date date = new java.sql.Date(new Date().getTime()); 
//构建一个java.sql.Timestamp对象
java.sql.Timestamp timestamp = new java.sql.Timestamp(new Date().getTime());

upStatement.setInt(1, 3);
upStatement.setString(2, "EXCEPTION_MESS");
upStatement.setTimestamp(3,timestamp);
upStatement.setDate(4, date);
upStatement.setLong(5, id);

upStatement.execute();
connection.commit();

upStatement.close();
connection.close();

 

public Connection getConnection(String beanId) {
	Connection conn = null;
	DataSource ds = DataSourceFactory.getDataSourceFactory(beanId);
	
	try {
		conn = ds.getConnection();
	} catch (SQLException e) {
		e.printStackTrace();
	}
	
	return conn;
}

 dataSourceContext.xml

<bean id="sclMiddleTble" class="org.apache.commons.dbcp.BasicDataSource" 
		destroy-method="close">
	<property name="driverClassName">
		<value>oracle.jdbc.OracleDriver</value>
	</property>
	 <property name="url">
			 <value>jdbc:oracle:thin:@localhost:1521:jqwlscl</value> 
	</property>
	<property name="username">
		<value>middleTable</value>
	</property>
	<property name="password">
		<value>middleTable</value>
	</property> 
	<property name="initialSize" value="30"/>
	<property name="maxActive" value="500"/> 
	<property name="maxIdle" value="2"/>
	<property name="minIdle" value="1"/>
 </bean>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值