JDBC Spring 之JdbcTemplate 增删改查操作
来源:互联网 发布:java流媒体框架 编辑:程序博客网 时间:2024/06/08 06:15
之前简单介绍了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 {
-
- Class.forName("com.mysql.jdbc.Driver");
- Properties prop = new Properties();
- InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
- prop.load(is);
-
- myDataSource = BasicDataSourceFactory.createDataSource(prop);
- } catch (Exception e) {
- throw new ExceptionInInitializerError(e);
- }
- }
-
-
- public static DataSource getDataSource() {
- return myDataSource;
- }
- public static Connection getConnection() throws SQLException {
-
- 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();
- }
- }
- }
- }
- }
然后接下来我们来使用JdbcTemplate这个Spring模板类来进行CRUD操作。(记得在此之前应该引入spring.jar架包)如图所示我所引入的包:
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 JdbcTemplate jdbc = new JdbcTemplate(JdbcUtils.getDataSource());
-
- public static void main(String[] args) {
-
-
- User user = new User();
-
-
-
-
- user.setId(10);
-
-
- 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;
- }
-
-
-
- }
0 0