PreparedStatement设置日期型参数

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>

 

 

 

你可能感兴趣的:(PreparedStatement设置日期型参数)