1. 程式人生 > >MySQL--使用C3P0連線池進行批量插入,刪除,更新資料的實現

MySQL--使用C3P0連線池進行批量插入,刪除,更新資料的實現

在我們實際開發中,肯定會遇到大批量的資料來進行入庫操作。

如果不使用框架應該怎麼進行資料的批量入庫呢。

使用preparedStatement的addBatch()和executeBatch()方法可以進行批量操作。

整體思路是:

1.批量插入首先需要構造一個updateSet,用來儲存User物件,建立一個updateSet,然後初始化資料。

再將updateSet中的資料批量進行插入。

2.批量刪除和批量修改需要先把資料庫中USER表中的資料都查出來,存入updateSet中,然後再執行刪除和更新的操作。

具體程式碼:

1.首先我們需要自己實現一個數據庫連線池。

public class C3P0Utils {

	// 建立資料來源
	private static ComboPooledDataSource datasoure = new ComboPooledDataSource();

	// 設定資料庫資訊
	static {
		try {
			datasoure.setDriverClass("com.mysql.jdbc.Driver");
			datasoure.setJdbcUrl(
					"jdbc:mysql://localhost:3306/testDB?user=root&password=1234&useUnicode=true&characterEncoding=UTF-8");
			datasoure.setUser("root");
			datasoure.setPassword("1234");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 獲取資料庫連線物件
	public static Connection getConnection() {
		Connection conn = null;
		try {
			conn = datasoure.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	// 關閉資料庫連線
	public static void close(Connection conn) {
		try {
			if (conn != null && conn.isClosed()) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

2.實體類User

public class User {

	private int id;//id
	private String name;//使用者名稱
	private int old;//年齡
	private String sex;//性別

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getOld() {
		return old;
	}

	public void setOld(int old) {
		this.old = old;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", old=" + old + ", sex=" + sex + "]";
	}

	public User(String name, int old, String sex) {
		super();
		this.name = name;
		this.old = old;
		this.sex = sex;
	}

	public User() {
		super();
	}
}

3.測試類(包含資料查詢,UpdateSet初始化,批量插入,刪除,更新)

public class C3P0Test {

	private static Connection conn = null;
	private static PreparedStatement ps = null;
	private static ResultSet rs = null;
	public static HashSet<User> userSet = new HashSet<>();

	public static void main(String[] args) {
		// 1.批量插入操作
		// getData();
		// insertData();
		// 2.批量更新操作
		// getDataByMySQL();
		// System.out.println(userSet.toString());
		// updateData();
		//3.批量刪除操作
		// getDataByMySQL();
		// System.out.println(userSet.toString());
		// deleteData();
	}

	// 查詢資料庫所有User資料
	private static void getDataByMySQL() {
		try {
			int count = 0;
			conn = C3P0Utils.getConnection();
			conn.setAutoCommit(false);
			String sql = "select * from USER";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				User user = new User();
				user.setId(rs.getInt(1));
				user.setName(rs.getString(2));
				user.setOld(rs.getInt(3));
				user.setSex(rs.getString(4));
				userSet.add(user);
				count++;
			}
			System.out.println("查詢到" + count + "條資料");
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				C3P0Utils.close(conn);
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

	// 初始化userSet
	public static void getData() {
		User u1 = new User("張三", 35, "男");
		User u2 = new User("李四", 25, "男");
		User u3 = new User("王五", 45, "女");
		User u4 = new User("趙六", 15, "女");
		userSet.add(u1);
		userSet.add(u2);
		userSet.add(u3);
		userSet.add(u4);
	}

	// 批量插入資料
	public static void insertData() {
		try {
			int count = 0;
			conn = C3P0Utils.getConnection();
			conn.setAutoCommit(false);
			String sql = "insert into USER(name,old,sex) value(?,?,?)";
			ps = conn.prepareStatement(sql);
			for (User user : userSet) {
				ps.setString(1, user.getName());
				ps.setInt(2, user.getOld());
				ps.setString(3, user.getSex());
				ps.addBatch();
				count++;
			}
			ps.executeBatch();
			ps.clearBatch();
			conn.commit();
			System.out.println("添加了" + count + "條資料");
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				C3P0Utils.close(conn);
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

	// 批量刪除資料
	public static void deleteData() {
		try {
			int count = 0;
			conn = C3P0Utils.getConnection();
			conn.setAutoCommit(false);
			String sql = "delete from USER where id =?";
			ps = conn.prepareStatement(sql);
			for (User user : userSet) {
				ps.setInt(1, user.getId());
				ps.addBatch();
				count++;
			}
			ps.executeBatch();
			ps.clearBatch();
			conn.commit();
			System.out.println("刪除了" + count + "條資料");
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				C3P0Utils.close(conn);
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	// 批量更新資料
	public static void updateData() {
		try {
			int count = 0;
			conn = C3P0Utils.getConnection();
			conn.setAutoCommit(false);
			String sql = "UPDATE USER SET old = 30 WHERE id = ?";
			ps = conn.prepareStatement(sql);
			for (User user : userSet) {
				ps.setInt(1, user.getId());
				ps.addBatch();
				count++;
			}
			ps.executeBatch();
			ps.clearBatch();
			conn.commit();
			System.out.println("更新了" + count + "條資料");
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				C3P0Utils.close(conn);
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

}
覺得有幫助的朋友請點個贊哦~~