2019独角兽企业重金招聘Python工程师标准>>>
###1.导入jar包,必须jar包:c3p0、mysql-connector、beans、context、dao、jdbc,截图如下
###2.db.properties
jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///test?useUnicode=true&characterEncoding=UTF8jdbc.initPoolSize=10
jdbc.maxPoolSize=50
###3.applicationContext.xml
###4.jdbcTest
package com.test.spring.jdbc;import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import javax.sql.DataSource;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;public class JdbcTest {private ApplicationContext ctx&#61; null;private JdbcTemplate jdbcTemplate;{ctx &#61; new ClassPathXmlApplicationContext("applicationContext.xml");jdbcTemplate &#61; (JdbcTemplate) ctx.getBean("jdbcTemplate");}/*** 单个 更新/插入/删除*/&#64;Testpublic void testUpdate(){String sql &#61; "update t_thing set content&#61;? where id&#61;?";jdbcTemplate.update(sql, "张三","1000000001");}/*** 批量 更新/插入/删除*/&#64;Testpublic void testBatchInsert(){/*** 注意&#xff1a;数据库字段是5个列&#xff1a;id,name,content,flag,addTime* 我将id设置为自增长&#xff0c;所以这里insert时不需要填写id*/String sql &#61; "insert into t_thing(name,content,flag,addTime) values(?,?,?,?)";List batchArgs &#61; new ArrayList<>();batchArgs.add(new Object[]{"王五","完成","1","20160904111700"});batchArgs.add(new Object[]{"赵六","没有完成","1","20160904111701"});jdbcTemplate.batchUpdate(sql, batchArgs);}/*** 查询单条记录* 注意&#xff1a;* 1.使用queryForObject(String sql, RowMapper
###5.bean
public class Thing {private Integer id;private String name;private String content;private int flag;private String addTime;
}
###6.数据库