之前简单介绍了JDBC CRUD的操作,接下来我为大家来讲解一下JDBC 使用spring框架来进行CURD(增删改查操作)使用Spring中的JdbcTemplate之前应该给一个数据源来获得连接。
- package cn.itcast.jdbc;
- import java.io.InputStream;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Properties;
- import javax.sql.DataSource;
- import org.apache.commons.dbcp.BasicDataSourceFactory;
- public final class JdbcUtils {
- private static String url = "jdbc:mysql://localhost:3306/JdbcTest";
- private static String user = "root";
- private static String password = "Qb393302+";
- private static DataSource myDataSource = null;
- private JdbcUtils() {
- }
- static {
- try {
- // 1.注册
- Class.forName("com.mysql.jdbc.Driver");
- Properties prop = new Properties();
- InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
- prop.load(is);
- //使用的是DBCP方式来加载数据库连接信息
- myDataSource = BasicDataSourceFactory.createDataSource(prop);
- } catch (Exception e) {
- throw new ExceptionInInitializerError(e);
- }
- }
- //获得数据源
- public static DataSource getDataSource() {
- return myDataSource;
- }
- public static Connection getConnection() throws SQLException {
- // return DriverManager.getConnection(url,user,password);
- return myDataSource.getConnection();
- }
- public static void free(ResultSet rs, Statement st, Connection conn) {
- try {
- if (rs != null)
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (st != null)
- st.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if (conn != null)
- try {
- conn.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
Spring -->CRUD操作:
- <span style="font-size:24px;">package cn.itcast.jdbc.spring;
- import java.util.Date;
- import org.springframework.jdbc.core.BeanPropertyRowMapper;
- import org.springframework.jdbc.core.JdbcTemplate;
- import cn.itcast.jdbc.JdbcUtils;
- import cn.itcast.jdbc.domain.User;
- public class JdbcTemplateCreate {
- //获取数据源(设置为static 是因为该jdbc多次被调用)
- static JdbcTemplate jdbc = new JdbcTemplate(JdbcUtils.getDataSource());
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //这里为测试CRUD代码块
- User user = new User();
- // user.setName("小Q");
- // user.setBirthday(new Date());
- // user.setMoney(3000);
- // text1(user);
- user.setId(10);
- // user.setName("BB");
- // delete(user);
- user = query(8);
- if (user == null) {
- System.out.println("查询失败");
- }else {
- System.out.println("查询成功");
- }
- }
- //插入,增加
- public static void insert(User user) {
- String sql = "insert into user(name,birthday,money)values(?,?,?)";
- Object args[] = {user.getName(),user.getBirthday(),user.getMoney()};
- int temp = jdbc.update(sql, args);
- if (temp > 0) {
- System.out.println("插入成功!");
- }else{
- System.out.println("插入失败");
- }
- }
- //删除
- public static void delete(int id) {
- String sql = "delete from user where id = ?";
- Object args[] = new Object[]{id};
- int temp = jdbc.update(sql,args);
- if (temp > 0) {
- System.out.println("删除成功");
- }else {
- System.out.println("删除失败");
- }
- }
- //更新
- public static void update(User user) {
- String sql = "update user set name = ? where id = ?";
- Object args[] = new Object[]{user.getName(),user.getId()};
- int temp = jdbc.update(sql,args);
- if (temp > 0) {
- System.out.println("更新成功");
- }else {
- System.out.println("更新失败");
- }
- }
- //查询
- public static User query(int id) {
- String sql = "select * from user where id = ?";
- Object args[] = new Object[]{id};
- Object user = jdbc.queryForObject(sql,args,new BeanPropertyRowMapper(User.class));
- return (User)user;
- }
- }
- </span>
上面的User类是事先已经创建好的。
- package cn.itcast.jdbc.domain;
- import java.util.Date;
- public class User {
- private int id;
- private String name;
- private Date birthday;
- private float money;
- public int getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public float getMoney() {
- return money;
- }
- public void setMoney(float money) {
- this.money = money;
- }
- public void setId(int id) {
- this.id = id;
- }
- }