public boolean doCreate(Member member) throws Exception {
String sql = "insert into member01(mid,name,age,phone,birthday,note) values(?,?,?,?,?,?)";
this.preparedStatement = this.connection.prepareStatement(sql);
this.preparedStatement.setString(1, member.getMid());
this.preparedStatement.setString(2, member.getName());
this.preparedStatement.setInt(3, member.getAge());
this.preparedStatement.setString(4, member.getPhone());
this.preparedStatement.setDate(5, new java.sql.Date(member.getBrithday().getTime()));
this.preparedStatement.setString(6, member.getNote());
return this.preparedStatement.executeUpdate() > 0;
}
public boolean doUpdate(Member member) throws Exception {
String sql = "update member01 set name=?,age=?,phone=?,birthday=?,note=? where mid=?";
this.preparedStatement = this.connection.prepareStatement(sql);
this.preparedStatement.setString(1, member.getName());
this.preparedStatement.setInt(2, member.getAge());
this.preparedStatement.setString(3, member.getPhone());
this.preparedStatement.setDate(4, new java.sql.Date(member.getBrithday().getTime()));
this.preparedStatement.setString(5, member.getNote());
this.preparedStatement.setString(6, member.getMid());
return this.preparedStatement.executeUpdate() > 0;
}
public boolean doRemoveBatch(Set<String> ids) throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append("delete from member01 where mid in(");
Iterator<String> iterator = ids.iterator();
while (iterator.hasNext()) {
buffer.append("'").append(iterator.next()).append(")");
}
buffer.delete(buffer.length() - 1, buffer.length()).append("");
this.preparedStatement = this.connection.prepareStatement(buffer.toString());
return this.preparedStatement.executeUpdate() == ids.size();
}
public Member findById(String id) throws Exception {
Member member = null;
String sql = "select mid,name,age,phone,birthday,note from member01 where mid=?";
this.preparedStatement = this.connection.prepareStatement(sql);
this.preparedStatement.setString(1, id);
ResultSet resultSet = this.preparedStatement.executeQuery();
if (resultSet.next()) {
member = new Member();
member.setMid(resultSet.getString(1));
member.setName(resultSet.getString(2));
member.setAge(resultSet.getInt(3));
member.setPhone(resultSet.getString(4));
member.setBrithday(resultSet.getDate(5));
member.setNote(resultSet.getString(6));
}
return member;
}
public List<Member> findByAll() throws Exception {
List<Member> members = new ArrayList<>();
String sql = "select mid,name,age,phone,birthday,note from member01";
this.preparedStatement = this.connection.prepareStatement(sql);
ResultSet resultSet = this.preparedStatement.executeQuery();
while (resultSet.next()) {
Member member = new Member();
member.setMid(resultSet.getString(1));
member.setName(resultSet.getString(2));
member.setAge(resultSet.getInt(3));
member.setPhone(resultSet.getString(4));
member.setBrithday(resultSet.getDate(5));
member.setNote(resultSet.getString(6));
members.add(member);
}
return members;
}
打开App,阅读手记
热门评论
原生的java mysql jdbc 没多少东西;为啥呢?对数据库的操作就是增删改查;写出花来就这么些东西;
1、方法的参数是一个java bean
2、方法的返回值
3、预编译sql
4、给占位符赋值
5、了解几个对象之间的关系,到底是如何创建出来的;那些是实例化出来的,那些是通过实例对象通过调用方法返回的;
6、Date 这个比较有意思
7、批量删除里面的参数,因为用到了集合,Iterator 接口遍历集合类的元素
8、动态字符串,最后一个逗号干死固定写法记住即可
9、通过ID 返回的是一个java bean 对象;返回啥,方法定义后,先声明一个对象接收返回的对象
10、如果是返回一堆数据,那就将数据都丢到集合中去