每天叫醒自己的不是闹钟,而是梦想
conn层
package conn; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import com.mysql.jdbc.Statement; public class Conn { /*Statement stmt; * 该对象用于执行静态SQL语句并返回它产生的结果。 默认情况下,只有一个ResultSet对象每Statement对象可以同时打开。因此,如果一个ResultSet对象的阅读是交错的另一个阅读,每个人都必须有不同的Statement对象产生。在Statement接口的所有执行方法隐式关闭的声明,目前ResultSet对象如果打开一个存在。 简单来说,就是一个结果对应一个Statement 本次实现增删改查。需要4个返回结果,所以定义4个Statement对象 */ Statement stmt; Statement stmt1; Statement stmt2; Statement stmt3; public Conn() //本段代码块构造函数,初始化连接数据库 { try { Class.forName("com.mysql.jdbc.Driver");//加载jdbc驱动 String url = "jdbc:mysql://localhost:3306/test?user=root&password=password&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false"; Connection con=DriverManager.getConnection(url);//连接数据库 stmt =(Statement) con.createStatement(); stmt1 =(Statement) con.createStatement(); stmt2 =(Statement) con.createStatement(); stmt3 =(Statement) con.createStatement(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public boolean insert(String sql) //插入。Statement对象调用executeupdate方法执行sql语句。 成功与否返回布尔型。 { boolean flag=true; //flag标识 try { stmt.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); flag=false;//捕获到异常。执行失败。返回false; } return flag; } public boolean update(String sql) { boolean flag=true; try { stmt1.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); flag=false; } return flag; } public boolean delect(String sql) { boolean flag=true; try { stmt2.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); flag=false; } return flag; } public ResultSet select(String sql) throws Exception { return stmt3.executeQuery(sql); } }
dao层
package dao; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import com.mysql.jdbc.ResultSetImpl; import bean.Student; import conn.Conn; /*本类中拼接sql语句 本类为实现接口方法 * 用于用户调用 * 传入conn连接数据库类 * 节后的实现后缀用impl * * */ public class StudentDao implements IStudentDao { Conn conn=new Conn(); @Override public boolean insertStudent(Student student) { // TODO Auto-generated method stub String name=student.getName(); int age=student.getAge(); int sex=student.getSex(); String sql = "INSERT INTO student(`name`,`age`,`sex`) VALUES('"+name+"',"+age+","+sex+");"; return conn.insert(sql); } @Override public boolean updateStudent(Student student) { int id=student.getId(); String name=student.getName(); int age=student.getAge(); int sex=student.getSex(); String sql = "UPDATE student set `name`='"+name+"',age="+age+",sex="+sex+" WHERE id="+id+""; // String sql = "UPDATE student SET `name`='"+name+"'"+",age="+age+",sex="+sex+" WHERE id="+id; return conn.update(sql); } @Override public boolean delectStudent(Student student) { // TODO Auto-generated method stub int id=student.getId(); String sql = "DELETE FROM student where id="+id; return conn.delect(sql); } @Override public ArrayList<Student> selectStudent() { // TODO Auto-generated method stub String sql="select *from student;"; ResultSet set=null; ArrayList<Student> list=new ArrayList<>(); try { set=conn.select(sql); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { while(set.next()) { Student student=new Student(); student.setId(set.getInt("id")); student.setName(set.getString("name")); student.setAge(set.getInt("age")); student.setSex(set.getInt("sex")); list.add(student); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } }
package dao; import java.util.ArrayList; import bean.Student; public interface IStudentDao { /*本接口写方法。用于其他类继承调用 * 使用其他包类是。需要先导入 * 查询结果返回数组 * 接口 * * */ public boolean insertStudent(Student student); public boolean updateStudent(Student student); public boolean delectStudent(Student student); public ArrayList<Student> selectStudent(); }
server
package server; import java.util.ArrayList; import bean.Student; public interface IStudentServer { public boolean insertStudent(Student student); public boolean deleteStudent(Student student); public boolean updateStudent(Student student); public ArrayList<Student> selectAllStudent(); }
package serverImpl; import java.util.ArrayList; import bean.Student; import dao.StudentDao; import server.IStudentServer; public class ServerStudent implements IStudentServer{ StudentDao studentDao= new StudentDao(); @Override public boolean insertStudent(Student student) { // TODO Auto-generated method stub return studentDao.insertStudent(student); } @Override public boolean deleteStudent(Student student) { // TODO Auto-generated method stub return studentDao.delectStudent(student); } @Override public boolean updateStudent(Student student) { // TODO Auto-generated method stub return studentDao.updateStudent(student); } @Override public ArrayList<Student> selectAllStudent() { // TODO Auto-generated method stub return studentDao.selectStudent(); } }
action
package action; import java.util.ArrayList; import bean.Student; import serverImpl.ServerStudent; public class StudentAction { ServerStudent serverStudent =new ServerStudent(); public String insertStudent(Student student) { boolean flag=serverStudent.insertStudent(student); if(flag==true) return "插入成功"; else return "插入失败"; } public String delectStudent(Student student) { boolean flag=serverStudent.deleteStudent(student); if(flag==true) return "刪除成功"; else return "刪除失败"; } public String updateStudent(Student student) { boolean flag=serverStudent.updateStudent(student); if(flag==true) return "修改成功"; else return "修改失败"; } public String selectStudent() { ArrayList<Student> student=serverStudent.selectAllStudent(); String ss = ""; for(Student chaoba:student) { ss += "id:"+chaoba.getId(); ss += " name:"+chaoba.getName(); ss += " age:" + chaoba.getAge(); ss += " sex:" + (chaoba.getSex()==1?"男":"女"); ss += " "; } return ss; } }
bean 业务实体
package bean; /*Student类。用做业务实体, * 进行复制。传递获取 * * * * * */ public class Student { private int id; private String name; private int age; private int 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 getAge() { return age; } public void setAge(int age) { this.age = age; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } }
*-------------------------------------------*
By:暮雪超霸博客:http://www.cnblogs.com/chaoba/
*-------------------------------------------*