运行环境:使用了dbcp数据源:
我用的JAR包有:
commons-collections-3.1.jar commons-dbcp.jar commons-pool.jar ojdbc14.jar(或class12.jar)
如果不知道哪里找JAR包的话 可以建一个普通的java工程,不是web工程哦!~
再右键-->myeclipse--add spring capabilities 引入spring就可以了
sql:
- drop table t_user;
- create table t_user
- (
- userid NUMBER not null,
- username VARCHAR2(100)
- );
- create sequence SEQ_T_USER
- minvalue 1
- maxvalue 999999999999999999999999999
- start with 1
- increment by 1
- cache 20;
jdbcTemplate:
- package com.jungle.test;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.List;
- import java.util.Properties;
- import javax.sql.DataSource;
- import org.apache.commons.dbcp.BasicDataSourceFactory;
- import org.springframework.jdbc.core.BeanPropertyRowMapper;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.jdbc.core.PreparedStatementSetter;
- import com.jungle.bean.User;
- /**
- * Spring模板(JdbcTemplater例子)
- * */
- public class JdbcTemplateTest {
- public static void main(String[] args) throws Exception {
- Properties ps = new Properties();
- ps.setProperty("url" , "jdbc:oracle:thin:@localhost:1521:oracle" );
- ps.setProperty("username" , "chenl" );
- ps.setProperty("password" , "chenl" );
- ps.setProperty("driverClassName" , "oracle.jdbc.driver.OracleDriver" );
- DataSource ds = BasicDataSourceFactory.createDataSource(ps);
- JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
- /*************************************** test ***********************/
- // 1.查
- System.out.println(query1(jdbcTemplate));
- // 2.条件查
- System.out.println(query2(jdbcTemplate, 1 ));
- // 3.改
- update(jdbcTemplate, new User( 1 , "aaaaaa" ));
- // 4.增
- User user = new User();
- user.setUsername("11hahaa" );
- insert(jdbcTemplate, user);
- // 5.删
- user = new User();
- user.setUserid(2 );
- del(jdbcTemplate, user);
- }
- /**
- * 1.使用 JdbcTemplate <br>
- * 查出所有
- * */
- @SuppressWarnings ({ "unchecked" , "rawtypes" })
- public static List<User> query1(JdbcTemplate jdbcTemplate) {
- String sql = "select * from t_user" ;
- List<User> list = (List<User>) jdbcTemplate.query(sql,
- new BeanPropertyRowMapper(User. class ));
- return list;
- }
- /**
- * 2.使用 JdbcTemplate<br>
- * 条件查出所有查出所有
- * */
- @SuppressWarnings ({ "unchecked" , "rawtypes" })
- public static List<User> query2(JdbcTemplate jdbcTemplate, int userid) {
- String sql = "select * from t_user where userid=?" ;
- List<User> list = (List<User>) jdbcTemplate.query(sql,
- new Object[] { userid }, new BeanPropertyRowMapper(User. class ));
- return list;
- }
- /**
- * 3.使用 JdbcTemplate<br>
- * 修改数据
- * */
- public static void update(JdbcTemplate jdbcTemplate, final User user) {
- String sql = "update t_user set username=? where userid=?" ;
- jdbcTemplate.update(sql, new PreparedStatementSetter() {
- @Override
- public void setValues(PreparedStatement ps) throws SQLException {
- ps.setString(1 , user.getUsername());
- ps.setInt(2 , user.getUserid());
- }
- });
- }
- /**
- * 4.使用 JdbcTemplate<br>
- * 插入数据
- * */