控制层Action接受从模型层DAO传来的数据,显现在视图层上。
package Action;import java.sql.Connection;import java.sql.SQLException;import java.sql.Statement;import DAO.StuDAO;import Model.Student;import Util.DBUtil;public class StuAction { public static void main(String args[]) throws Exception { Connection conn = DBUtil.getConnection(); Statement stmt = conn.createStatement();// ResultSet rs = stmt.executeQuery("select * from stuInfoTable"); // while(rs.next()) { System.out.println(rs.getInt(1)+" "+ rs.getString(2)+" "+rs.getString(3)); } //不是直接 Student stu = null,这样试空指针 Student stu = new Student(); stu.setStuID(5); stu.setName("王五"); stu.setSex("女"); StuDAO sd = new StuDAO(); //sd.addStu(stu); // stu.setName("马六");// stu.setSex("男");// sd.updateStu(stu); sd.delStu(stu.getStuID()); } //直接调用StuDAO的方法来更新 增加 删除 public void add(Student stu) throws Exception { StuDAO sd = new StuDAO(); sd.addStu(stu); } //通过控制层来 链接视图层}
package DAO;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import Model.Student;import Util.DBUtil;public class StuDAO { Connection conn = DBUtil.getConnection(); //一个一个传参数很麻烦 直接传一个对象 //如果stuID设置了自动递增 就不需要插入了 public void addStu(Student stu) throws Exception { String sql ="" + "insert into stuInfoTable" + "(" + " stuID,name,sex"+")" + "values(" + "?,?,?)"; //这样的语句可以预加载在服务器里 execute后才会执行 PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, stu.getStuID()); ps.setString(2, stu.getName()); ps.setString(3, stu.getSex()); ps.execute(); } //和增加的逻辑一直 所以直接复制上述代码 然后修改sql语句 //可以直接传过来stuID,但是这样需要根据ID去找Student public void updateStu(Student stu) throws Exception { //不加where语句的话 所有的都会更新 //注意下面的语句 一定要有空格 String sql ="" + "update stuInfoTable " + "set " + "name=?,sex=? " + "where "+ "stuID=?"; //这样的语句可以预加载在服务器里 execute后才会执行 PreparedStatement ps = conn.prepareStatement(sql); //打印出来sql语句 发现 中间少了 空格 然后修改上面的sql语句 System.out.println(sql); //注意下面的 序号 要和update的序号一致 ps.setString(1, stu.getName()); ps.setString(2, stu.getSex()); ps.setInt(3, stu.getStuID()); ps.execute(); } public void delStu(int stuID) throws Exception { String sql ="" + "delete stuInfoTable " +"where "+ "stuID=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, stuID); ps.execute(); } //查询学生集合 public ListqueryStu() throws Exception { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from stuInfoTable"); List stuList = new ArrayList (); Student stu = null; while(rs.next()) { stu = new Student(); stu.setStuID(rs.getInt(1)); stu.setName(rs.getString(2)); stu.setSex(rs.getString(3)); stuList.add(stu); } return stuList; } //查询一个学生 //应该需要和上面重载的 但是为了做实验 先写完所有方法 再去实现 public Student queryOneStu() { return null; }}
package Model;public class Student { int stuID; String name; String sex; public int getStuID() { return stuID; } public void setStuID(int stuID) { this.stuID = stuID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
package Util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import DAO.StuDAO;import Model.Student;public class DBUtil { private static Connection conn; static { String url = "jdbc:sqlserver://localhost:1433;databasename=stuDB"; try { // 1.注册驱动\ // Class.forName("com.mysql.jdbc.Driver"); Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 2.获得数据链接 conn = DriverManager.getConnection(url, "sa", "123456"); } catch (ClassNotFoundException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getConnection() { return conn; }}