标签:
经过前几篇的摸爬滚打,下面我们就开始我们真正的数据库操作了,本篇重点在于如何在网站端编写数据库操作语句,内容不多,就是我们常见的增删改查。
0、数据库对象创建:
在JAVASE基础知识总结时,就为大家点明了,JAVA是一种面向对象编程语言,所以我们再进行数据库创建之前,要先创建一个对象类。
/* * 实例化数据库中的对象 */ public class Student { private int id;//学生ID private String name;//学生姓名 private int sex;//学生性别 private int year;//学生年龄 private String from;//学生故乡 private String school;//学生所在学校 //为属性添加set()、get()方法 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 getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } }
1、接口创建:
这里采用了面向接口编程,方便我们管理我们的数据库操作方法。
/* * 数据库操作方法接口 */ public interface StudentMa { public boolean add(Student student);//添加 public boolean del(int id);//删除 public boolean update(Student student);//修改 public Student getById(int id);//精确查询 public List<Student> getByName(String name);//模糊查询 public List<Student> getAll();//遍历 }
2、增加操作:
如何通过JAVA语句为数据库添加数据呢?下面就是具体的方法实现。
public boolean add(Student student) { boolean flag = false; Connection conn = null; PreparedStatement pst = null; conn = DBO.getConnection(); String sql = "insert into students (name,sex,year,from,school) values (?,?,?,?,?)";//‘?‘表示占位符 try { pst = conn.prepareStatement(sql); pst.setString(1, student.getName()); pst.setInt(2, student.getSex()); pst.setInt(3, student.getYear()); pst.setString(4, student.getFrom()); pst.setString(5, student.getSchool()); int n = pst.executeUpdate(); if(n>0){ flag = true; } } catch (SQLException e) { e.printStackTrace(); } return flag; }
3、删除操作:
如何通过JAVA语句删除数据库中的数据呢?下面就是具体的方法实现。
public boolean del(int id) { boolean flag = false; Connection conn = null; PreparedStatement pst = null; conn = DBO.getConnection(); String sql = "delete from students where id="+id; try { pst = conn.prepareStatement(sql); int n = pst.executeUpdate(); if(n>0){ flag = true; } } catch (SQLException e) { e.printStackTrace(); } return flag; }
4、修改操作:
如何通过JAVA语句修改数据库中的数据呢?下面就是具体的方法实现。
public boolean update(Student student) { boolean flag = false; Connection conn = null; PreparedStatement pst = null; conn = DBO.getConnection(); String sql = "update students set name=?,sex=?,year=?,from=?,school=? where id=?";//‘?‘表示占位符 try { pst = conn.prepareStatement(sql); pst.setString(1, student.getName()); pst.setInt(2, student.getSex()); pst.setInt(3, student.getYear()); pst.setString(4, student.getFrom()); pst.setString(5, student.getSchool()); pst.setInt(6, student.getId()); int n = pst.executeUpdate(); if(n>0){ flag = true; } } catch (SQLException e) { e.printStackTrace(); } return flag; }
5、查询操作:
a、精确查询:
如何通过JAVA语句精确查找数据库中的数据呢?下面就是具体的方法实现。
public Student getById(int id) { Student student = new Student(); Connection conn = null; Statement st = null; ResultSet rs = null; conn = DBO.getConnection(); String sql = "select * from students where id="+id; try { st = conn.createStatement(); rs = st.executeQuery(sql); while(rs.next()){ student.setId(id); student.setName(rs.getString("name")); student.setSex(rs.getInt("sex")); student.setYear(rs.getInt("year")); student.setFrom(rs.getString("from")); student.setSchool(rs.getString("school")); } } catch (SQLException e) { e.printStackTrace(); } return student; }
b、模糊查询:
如何通过JAVA语句模糊查找数据库中的数据呢?下面就是具体的方法实现。
public List<Student> getByName(String name) { List<Student> list = null; Connection conn = null; PreparedStatement pst = null; ResultSet rs = null; conn = DBO.getConnection(); String sql = "select * from students where name like ?"; try { pst = conn.prepareStatement(sql); pst.setString(1, "%"+name+"%"); rs = pst.executeQuery(); while(rs.next()){ Student student = new Student(); student.setId(rs.getInt("id")); student.setName(rs.getString("name")); student.setSex(rs.getInt("sex")); student.setYear(rs.getInt("year")); student.setFrom(rs.getString("from")); student.setSchool(rs.getString("school")); list.add(student); } } catch (SQLException e) { e.printStackTrace(); } return list; }
6、遍历操作:
如何通过JAVA语句遍历数据库中的数据呢?下面就是具体的方法实现。
public List<Student> getAll() { List<Student> list = null; Connection conn = null; Statement st = null; ResultSet rs = null; conn = DBO.getConnection(); String sql = "select * from students"; try { st = conn.createStatement(); rs = st.executeQuery(sql); while(rs.next()){ Student student = new Student(); student.setId(rs.getInt("id")); student.setName(rs.getString("name")); student.setSex(rs.getInt("sex")); student.setYear(rs.getInt("year")); student.setFrom(rs.getString("from")); student.setSchool(rs.getString("school")); list.add(student); } } catch (SQLException e) { e.printStackTrace(); } return list; }
好了到这里,关于数据库的增删改查操作的JAVA实现已经为大家总结完毕。下一篇select实现jsp页面与数据库交互。
标签:
原文地址:http://www.cnblogs.com/AndroidJotting/p/4343029.html