数据库表:
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50717
Source Host : 127.0.0.1:3306
Source Database : test
Target Server Type : MYSQL
Target Server Version : 50717
File Encoding : 65001
Date: 2020-06-23 10:44:51
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`phone` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `union_unique_name_phone` (`name`,`phone`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '张三', '123456', '12580');
INSERT INTO `user` VALUES ('2', '李四', '654321', '13333333333');
用户实体类:
public class User {
private Integer id;
private String name;
private String password;
private String phone;
public User() {
}
public User(Integer id, String name, String password, String phone) {
this.id = id;
this.name = name;
this.password = password;
this.phone = phone;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
依赖(版本根据自己本地的自行选择):
mysql
mysql-connector-java
5.1.29
BaseDao源码,根据需要自行调整:
public class BaseDao {
private String dirverClassName="com.mysql.jdbc.Driver";
private String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=true";
private String userName="root";
private String password="root";
private Connection conn=null;
private void getConnection(){
if(conn==null){
//1、加载驱动
try {
Class.forName(dirverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//2、创建连接
try {
conn = DriverManager.getConnection(url,userName,password);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public int common(String sql,Object[] params){
int changeRow=0;
PreparedStatement preparedStatement = null;
try {
preparedStatement = conn.prepareStatement(sql);
if(params!=null){
for (int i = 1; i <=params.length ; i++) {
preparedStatement.setObject(i,params[i-1]);
}
}
changeRow = preparedStatement.executeUpdate();
closeAll(null,preparedStatement,null);//此处不关闭连接
} catch (SQLException e) {
e.printStackTrace();
}
return changeRow;
}
public int add(String sql,Object[] params){
return common(sql,params);
}
public int remove(String sql,Object[] params){
return common(sql,params);
}
public int update(String sql,Object[] params){
return common(sql,params);
}
public List query(String sql, Object[] params){
int changeRow=0;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List userList = new ArrayList<>();
try {
preparedStatement = conn.prepareStatement(sql);
if(params!=null){
for (int i = 1; i <=params.length ; i++) {
preparedStatement.setObject(i,params[i-1]);
}
}
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String password = resultSet.getString("password");
String phone = resultSet.getString("phone");
userList.add(new User(id,name,password,phone));
}
} catch (SQLException e) {
e.printStackTrace();
}
return userList;
}
public void closeAll(ResultSet resultSet,PreparedStatement preparedStatement,Connection conn){
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
通过connection可以控制事务,连接数可以通过getConnection来控制,可以自行实现连接池(数组,列表,策略可以选择轮询、随机);
此处转载自:https://blog.csdn.net/xulianboblog/article/details/51135677,这位博主说的很清楚了,自己就不敲了
方法executeUpdate
用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQL DDL(数据定义语言)语句,例如 CREATE TABLE 和 DROP TABLE。INSERT、UPDATE 或 DELETE 语句的效果是修改表中零行或多行中的一列或多列。executeUpdate 的返回值是一个整数(int),指示受影响的行数(即更新计数)。对于 CREATE TABLE 或 DROP TABLE 等不操作行的语句,executeUpdate 的返回值总为零。
方法execute
可用于执行任何SQL语句,返回一个boolean值,表明执行该SQL语句是否返回了ResultSet。如果执行后第一个结果是ResultSet,则返回true,否则返回false。但它执行SQL语句时比较麻烦,通常我们没有必要使用execute方法来执行SQL语句,而是使用executeQuery或executeUpdate更适合,但如果在不清楚SQL语句的类型时则只能使用execute方法来执行该SQL语句了 。
显然方法execute多了判断是否有结果集返回。