view層做用: 視圖層,即項目中的界面java
controller層做用: 控制層, 獲取界面上的數據,爲界面設置數據; 將要實現的功能交給業務層處理mysql
service層做用: 業務層, 功能的實現, 與controller控制層和數據訪問層DAO交互, 將對數據庫的操做交給DAO數據訪問層來處理sql
dao層做用: 數據訪問層, 用來操做數據庫表的數據數據庫
db數據庫: 這裏指MySQLdom
domain 實體包: 存放JavaBeanide
tools工具包:存放項目中使用到的工具類工具
test 測試包: 存放項目功能測試的代碼測試
以商店帳戶管理,貨物管理功能爲例簡歷的java項目ui
dao層鏈接數據庫數據庫語句--------------》service層 功能的實現-----------------------》controller層---------------view層this
tools:裝的是JDBCUtils
package tools; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class JDBCUtils { public static Connection getConn() { // 1.註冊驅動 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 2.得到鏈接對象 String url = "jdbc:mysql://localhost:3306/smalmarket?characterEncoding=utf-8"; String username = "root"; String password = "123"; Connection conn=null; try { conn = DriverManager.getConnection(url, username, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void close(Connection conn,PreparedStatement pst){ if(pst!=null){ try { pst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void close(Connection conn, PreparedStatement pst,ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(pst!=null){ try { pst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
view層:
package view; import java.util.List; import java.util.Scanner; import controller.SortController; import controller.UserController; import domain.Sort; public class MainView { private UserController userController=new UserController(); private SortController sortController=new SortController(); public void MainShow(){ System.out.println("-----歡迎進入商城-----"); System.out.println("一、登錄"); System.out.println("二、註冊"); System.out.println("三、退出"); System.out.println("四、請輸入您的選擇"); } public void IndexShow(){ System.out.println("----首頁----"); System.out.println("一、新增分類"); System.out.println("二、修改分類"); System.out.println("三、刪除分類"); System.out.println("四、查詢全部分類"); System.out.println("五、返回上級菜單"); System.out.println("六、請輸入您的選擇"); } //登錄 public void login(){ System.out.println("----登陸頁面----"); System.out.println("請輸入用戶名"); Scanner sc=new Scanner(System.in); String username=sc.next(); System.out.println("請輸入密碼"); String password=sc.next(); //調用Controller層的登陸方法 int count=userController.login(username,password); if(count>0){ System.out.println("登錄成功"); run2(); }else{ System.out.println("登錄失敗!"); } } //一級菜單 public void run(){ Scanner sc=new Scanner(System.in); while(true){ MainShow(); int choose=sc.nextInt(); switch(choose){ case 1: login(); break; case 2:register(); break; case 3: System.out.println("退出成功"); return; } } } //二級菜單 public void run2(){ Scanner sc=new Scanner(System.in); while(true){ IndexShow(); int choose=sc.nextInt(); switch(choose){ case 1:addSort(); break; case 2:update(); break; case 3:delete(); break; case 4:getall(); break; case 5: return; } } } //新增分類頁面 public void addSort(){ Scanner sc=new Scanner(System.in); System.out.println("=====新增頁面===="); System.out.println("請輸入你要新增的分類名稱"); String sname=sc.next(); //調用controller新增分類的方法 String mes=sortController.addSort(sname); System.out.println(mes); } //查詢全部分類頁面 public void getall(){ System.out.println("查詢全部分類"); //調用Controller調用方法 List<Sort>list=sortController.getall(); System.out.println("分類編號\t分類名稱"); for(Sort s:list){ System.out.println(s.getSid()+"\t"+s.getSname()); } } //修改分類頁面 public void update(){ getall(); System.out.println("請輸入您要修改的分類編號"); Scanner sc=new Scanner(System.in); int sid=sc.nextInt(); System.out.println("請輸入您要修改的分類名稱"); String sname=sc.next(); //調用 controller方法 String mes=sortController.updateSort(sid, sname); System.out.println(mes); } //刪除元素 public void delete(){ getall(); System.out.println("請輸入您要刪除的分類編號"); Scanner sc=new Scanner(System.in); int sid=sc.nextInt(); //調用controller方法 String mes=sortController.deleteSort(sid); System.out.println(mes); } //註冊 public void register(){ System.out.println("----註冊頁面----"); System.out.println("請輸入您要註冊的用戶名"); Scanner sc=new Scanner(System.in); int count=1; String uname=null; while(count>0){ uname=sc.next(); //調用controller的驗證用戶名是否已存在 count=userController.checkUsername(uname); if(count>0){ System.out.println("用戶已存在,請從新輸入"); } } System.out.println("請輸入密碼"); String pwd=sc.next(); //調用controller方法 String mes=userController.register(uname,pwd); System.out.println(mes); } }
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import domain.User; import tools.JDBCUtils; public class UserDao { //登陸驗證用戶名或密碼是否正確 public int login(String username,String password) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="select count(*) from user where uname=? and pwd=?"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, username); pst.setString(2, password); ResultSet rs=pst.executeQuery(); int count=0; while(rs.next()){ count=rs.getInt(1); } return count; } //驗證用戶名是否重複 public int checkUsername(String uname) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="select count(*) from user where uname=?"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, uname); ResultSet rs=pst.executeQuery(); //處理結果集 int count=0; while(rs.next()){ count=rs.getInt(1); } JDBCUtils.close(conn, pst,rs); return count; } //註冊 public int register(User user) throws SQLException{ Connection conn=JDBCUtils.getConn();//數據庫鏈接語句 String sql="insert into user(uname,pwd) values(?,?)"; PreparedStatement pst=conn.prepareStatement(sql);//sql語句注入問題(1'or'1=1) pst.setString(1, user.getUname());//第一個問號,的值是user.getUname(),從1開始 pst.setString(2, user.getPwd()); int row = pst.executeUpdate(); JDBCUtils.close(conn, pst);//釋放資源, return row; } }
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import domain.User; import tools.JDBCUtils; public class UserDao { //登陸驗證用戶名或密碼是否正確 public int login(String username,String password) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="select count(*) from user where uname=? and pwd=?"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, username); pst.setString(2, password); ResultSet rs=pst.executeQuery(); int count=0; while(rs.next()){ count=rs.getInt(1); } return count; } //驗證用戶名是否重複 public int checkUsername(String uname) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="select count(*) from user where uname=?"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, uname); ResultSet rs=pst.executeQuery(); //處理結果集 int count=0; while(rs.next()){ count=rs.getInt(1); } JDBCUtils.close(conn, pst,rs); return count; } //註冊 public int register(User user) throws SQLException{ Connection conn=JDBCUtils.getConn();//數據庫鏈接語句 String sql="insert into user(uname,pwd) values(?,?)"; PreparedStatement pst=conn.prepareStatement(sql);//sql語句注入問題(1'or'1=1) pst.setString(1, user.getUname());//第一個問號,的值是user.getUname(),從1開始 pst.setString(2, user.getPwd()); int row = pst.executeUpdate(); JDBCUtils.close(conn, pst);//釋放資源, return row; } }
package controller; import domain.User; import service.UserService; public class UserController { //一、封裝前臺數據傳給後臺 //二、接收後臺數據傳給前臺 private UserService userService=new UserService(); public int login(String username,String password){ return userService.login(username, password); } //驗證用戶名是否存在 public int checkUsername(String uname){ return userService.checkUsername(uname); } //註冊用戶 public String register(String uname,String pwd){ //封裝前臺數據 User user=new User (); user.setUname(uname); user.setPwd(pwd); return userService.register(user); } }
package domain; public class User { private int uid; private String uname; private String pwd; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User [uid=" + uid + ", uname=" + uname + ", pwd=" + pwd + "]"; } }
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import domain.Sort; import tools.JDBCUtils; public class SortDao { //新增分類 public int addSort(String sname) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="insert into sort(sname)values(?)"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, sname); int row = pst.executeUpdate(); JDBCUtils.close(conn, pst); return row; } //查詢全部分類 public List<Sort>getall() throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="select * from sort"; PreparedStatement pst=conn.prepareStatement(sql); ResultSet rs=pst.executeQuery(); //處理結果集 List<Sort>list=new ArrayList<Sort>(); while(rs.next()){ Sort sort=new Sort(); sort.setSid(rs.getInt("sid")); sort.setSname(rs.getString("sname")); list.add(sort); } //釋放資源 JDBCUtils.close(conn, pst, rs); return list; } //修改分類方法 public int updateSort(int sid,String sname) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="update sort set sname=? where sid=?"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, sname); pst.setInt(2, sid); int row=pst.executeUpdate(); JDBCUtils.close(conn, pst); return row; } //刪除分類 public int deleteSort(int sid) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="delete from sort where sid=?"; PreparedStatement pst=conn.prepareStatement(sql); pst.setInt(1, sid); int row=pst.executeUpdate(); JDBCUtils.close(conn, pst); return row; } }
package service; import java.sql.SQLException; import java.util.List; import dao.SortDao; import domain.Sort; public class SortService { private SortDao sortDao=new SortDao(); //新增分類 public String addSort(String sname){ int row=0; String mes=""; try { row=sortDao.addSort(sname); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(row>0){ mes="新增成功"; }else{ mes="新增失敗"; } return mes; } public List<Sort> getall(){ List<Sort> list=null; try { list=sortDao.getall(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } public String updateSort(int sid,String sname){ int row=0; String mes=""; try { row=sortDao.updateSort(sid,sname); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(row>0){ mes="修改爲功"; }else{ mes="修改失敗"; } return mes; } //刪除 public String deleteSort(int sid){ int row=0; String mes=""; try { row=sortDao.deleteSort(sid); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(row>0){ mes="刪除成功"; }else{ mes="刪除失敗"; } return mes; } }
package controller; import java.util.List; import domain.Sort; import service.SortService; public class SortController { private SortService sortService=new SortService(); //新增 public String addSort(String sname){ return sortService.addSort(sname); } //查詢 public List<Sort> getall(){ return sortService.getall(); } //修改分類 public String updateSort(int sid,String sname){ return sortService.updateSort(sid,sname); } //刪除分類 public String deleteSort(int sid){ return sortService.deleteSort(sid); } }
package domain; public class Sort { private int sid; private String sname; public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } @Override public String toString() { return "Sort [sid=" + sid + ", sname=" + sname + "]"; } }
package test; import view.MainView; public class Demo { public static void main(String[] args) { new MainView().run(); } }