druidとJdbcTemplateによるデータベースの削除・変更
1504 ワード
接続プール技術を利用してJDBCを実現する手順:
1.druid
2.JdbcTemplate
1.druid
1、 jar 。(druid-1.0.9.jar)
2、 。(druid.properties)
driverClassName
url
username
password
initialSize
maxActive
maxWait
2.JdbcTemplate
1、 。(mysql-connector-java-5.1.18-bin.jar,c3p0-0.9.1.2.jar,commons-beanutils-1.8.3.jar,spring-jdbc-4.2.4.RELEASE.jar,spring-core-4.2.4.RELEASE.jar,spring-beans-4.2.4.RELEASE.jar)
2、 (JDBCUtils)
1. DataSource ( )
2. Properties ( properties )
3.InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties") ClassLoader ,
4.pro.load(is)
5.ds = DruidDataSourceFactory.createDataSource(pro)
6. , ds.
7. Connection , ds.getConnection().
3、
1.private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); JdbcTemplate
2. sql , ?
3. template
:List users = template.query(sql, new BeanPropertyRowMapper(User.class)) , 。
:User user = template.queryForObject(sql, new BeanPropertyRowMapper(User.class), id)
:template.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail())
:template.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail(), user.getId())
:template.update(sql, id)