一个简单的MVC模式练习
生活随笔
收集整理的這篇文章主要介紹了
一个简单的MVC模式练习
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
控制層Action接受從模型層DAO傳來的數(shù)據,顯現(xiàn)在視圖層上。
?
?
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();//一個一個傳參數(shù)很麻煩 直接傳一個對象//如果stuID設置了自動遞增 就不需要插入了public void addStu(Student stu) throws Exception {String sql ="" + "insert into stuInfoTable" + "(" +" stuID,name,sex"+")" + "values(" + "?,?,?)";//這樣的語句可以預加載在服務器里 execute后才會執(zhí)行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去找Studentpublic void updateStu(Student stu) throws Exception {//不加where語句的話 所有的都會更新//注意下面的語句 一定要有空格String sql ="" + "update stuInfoTable " + "set " +"name=?,sex=? " + "where "+ "stuID=?";//這樣的語句可以預加載在服務器里 execute后才會執(zhí)行PreparedStatement ps = conn.prepareStatement(sql);//打印出來sql語句 發(fā)現(xiàn) 中間少了 空格 然后修改上面的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 List<Student> queryStu() throws Exception {Statement stmt = conn.createStatement();ResultSet rs = stmt.executeQuery("select * from stuInfoTable");List<Student> stuList = new ArrayList<Student>();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;}//查詢一個學生//應該需要和上面重載的 但是為了做實驗 先寫完所有方法 再去實現(xiàn)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.獲得數(shù)據鏈接conn = DriverManager.getConnection(url, "sa", "123456");} catch (ClassNotFoundException | SQLException e) {// TODO Auto-generated catch block e.printStackTrace();}}public static Connection getConnection() {return conn;} }?
總結
以上是生活随笔為你收集整理的一个简单的MVC模式练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 配置 mybatis的 log4j.pr
- 下一篇: ELKstack-Elasticsear