首页
package com.oracle.view; import java.util.Scanner; public class MainView { public void menu(){ System.out.println("---------欢迎光临我的超市-------"); System.out.println("1.注册"); System.out.println("2.登录"); System.out.println("3.退出"); System.out.println("请输入你的选择:"); } //一级菜单 public void mainMenu(){ Scanner sc=new Scanner(System.in); while(true){ menu(); int choose=sc.nextInt(); switch (choose) { case 1://注册方法 new UserView().zhuce(); break; case 2://登录 new UserView().login(); break; case 3: return; default: System.out.println("输入有误,重新输入"); break; } } } }
登录后页面
package com.oracle.view; import java.util.List; import java.util.Scanner; import com.oracle.controller.SortController; import com.oracle.domain.Sort; public class SortView { private SortController sortController=new SortController(); //菜单 public void show(){ System.out.println("-------分类管理--------"); System.out.println("1.新增分类"); System.out.println("2.修改分类"); System.out.println("3.删除分类"); System.out.println("4.查询所有分类"); System.out.println("5.返回上一级菜单"); System.out.println("请输入你的选择:"); } //二级菜单 public void menu(){ Scanner sc=new Scanner(System.in); while(true){ show(); int choose=sc.nextInt(); switch (choose) { case 1://新增 add(); break; case 2://修改 update(); break; case 3://删除 delete(); break; case 4://查询所有分类 getAll(); break; case 5://返回上一级菜单 return; default: System.out.println("输入有误"); break; } } } //新增分类页面 public void add(){ System.out.println("请输入分类名称:"); Scanner sc=new Scanner(System.in); String sname=sc.next(); System.out.println("请输入分类描述:"); String sdesc=sc.next(); //调用SortController中的新增分类方法 int row=sortController.add(sname, sdesc); if(row>0){ System.out.println("新增成功"); } else{ System.out.println("新增失败"); } } //修改 public void update(){ System.out.println("请输入要修改密码的分类名称"); Scanner sc=new Scanner(System.in); String sname=sc.next(); System.out.println("请输入要修改的密码"); String sdesc=sc.next(); int row=sortController.update(sname, sdesc); if(row>0){ System.out.println("修改成功"); }else{ System.out.println("修改失败"); } } //删 public void delete(){ System.out.println("请输入要删除的分类名称:"); Scanner sc=new Scanner(System.in); String sname=sc.next(); String sdesc = null; int row=sortController.delete(sname, sdesc); if(row>0){ System.out.println("删除成功"); }else{ System.out.println("删除失败"); } } //查询 public void getAll(){ List<Sort> list=sortController.getAll(); System.out.println("分类名称 分类名称 分类描述 "); for(Sort sort:list){ System.out.println(sort.getSid()+" "+sort.getSname()+" "+sort.getSdesc()); } } }
封装数据
package com.oracle.controller; import java.util.List; import com.oracle.domain.Sort; import com.oracle.service.SortService; public class SortController { //新增分类方法 private SortService sortService=new SortService(); public int add(String sname,String sdesc){ //封装数据 Sort sort=new Sort(); sort.setSname(sname); sort.setSdesc(sdesc); //调用SortService中的新增分类方法 int row=sortService.add(sort); return row; } //修改 public int update(String sname,String sdesc){ Sort sort=new Sort(); sort.setSname(sname); sort.setSdesc(sdesc); int row=sortService.update(sort); return row; } //删除 public int delete(String sname,String sdesc){ Sort sort=new Sort(); sort.setSname(sname); sort.setSdesc(sdesc); int row=sortService.delete(sort); return row; } //查询所有 public List<Sort> getAll(){ return sortService.getAll(); } }
package com.oracle.service; import java.sql.SQLException; import java.util.List; import com.oracle.dao.SortDao; import com.oracle.domain.Sort; public class SortService { private SortDao sortDao=new SortDao(); //新增分类 public int add(Sort sort){ int row=0; try { row = sortDao.add(sort); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return row; } public int update(Sort sort){ int row=0; try { row=sortDao.update(sort); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return row; } public int delete(Sort sort){ int row=0; try { row=sortDao.delete(sort); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return row; } public List<Sort> getAll(){ List<Sort> list=null; try { list=sortDao.getAll(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } }
连接数据库
package com.oracle.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 javax.swing.JButton; import com.oracle.domain.Sort; import com.oracle.tools.JDBCUtils; //dao层不处理异常,在service里处理 public class SortDao { //新增分类 public int add(Sort sort) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="insert into sort(sname,sdesc) values(?,?)"; PreparedStatement pst=conn.prepareStatement(sql); //给占位符赋值 pst.setString(1, sort.getSname()); pst.setString(2, sort.getSdesc()); //执行sql语句 int row=pst.executeUpdate(); //释放 JDBCUtils.close(conn, pst); return row; } //修改 public int update(Sort sort) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="update sort set sdesc=? where sname=?"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, sort.getSdesc()); pst.setString(2, sort.getSname()); int row=pst.executeUpdate(); JDBCUtils.close(conn, pst); return row; } //删除 public int delete(Sort sort) throws SQLException { Connection conn=JDBCUtils.getConn(); String sql="delete from sort where sname=?"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, sort.getSname()); 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")); sort.setSdesc(rs.getString("sdesc")); list.add(sort);//添加到集合里 } return list; } }
sort对象
package com.oracle.domain; public class Sort { private int sid; private String sname; private String sdesc; 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; } public String getSdesc() { return sdesc; } public void setSdesc(String sdesc) { this.sdesc = sdesc; } @Override public String toString() { return "Sort [sid=" + sid + ", sname=" + sname + ", sdesc=" + sdesc + "]"; } }
package com.oracle.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(){ Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/shop?characterEncoding=utf8"; String uname="root"; String ped="123456"; conn=DriverManager.getConnection(url,uname,ped); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { 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(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(); } } if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
测试
package com.oracle.test; import com.oracle.view.MainView; public class Demo { public static void main(String[] args) { new MainView().mainMenu(); } }