简介:使用JDBC、MySQL等,实现对女神信息的增删改查等。
主界面:
欢迎来到女神禁区:
下面是女神禁区的功能列表:
[MAIN/M]:主菜单
[QUERY/Q]:查看全部女神的信息
[GET/G]:查看某位女神的详细信息
[ADD/A]:添加女神信息
[UPDATE/U]:更新女神信息
[DELETE/D]:删除女神信息
[SEARCH/S]:查询女神信息(根据姓名、手机号来查询)
[EXIT/E]:退出女神禁区
[BREAK/B]:退出当前功能,返回主菜单
项目实现:
应用 MVC 三层搭建实现模型。
M:建立模型Goddess
public class Goddess {private Integer id;private String user_name;private Integer sex;private Integer age;private Date birthday;private String email;private String mobile;private String create_user;private String update_user;private Date create_date;private Date update_date;private Integer isdel;
}
对变量进行封装。
功能实现:
public class GoddessDao {public void addGoddess(Goddess g) throws Exception{Connection connConnection();String sql="" +"insert into imooc_goddess" +"(user_name,sex,age,birthday,email,mobile," +"create_user,create_date,update_user,update_date,isdel)" +"values(" +"?,?,?,?,?,?,?,current_date(),?,current_date(),?)";PreparedStatement ptmt=conn.prepareStatement(sql);ptmt.setString(1, g.getUser_name());ptmt.setInt(2, g.getSex());ptmt.setInt(3, g.getAge());ptmt.setDate(4, new Birthday().getTime()));ptmt.setString(5, g.getEmail());ptmt.setString(6, g.getMobile());ptmt.setString(7, g.getCreate_user());ptmt.setString(8, g.getUpdate_user());ptmt.setInt(9, g.getIsdel());ute();}public void updateGoddess(Goddess g) throws SQLException{Connection connConnection();String sql="" +" update imooc_goddess " +" set user_name=?,sex=?,age=?,birthday=?,email=?,mobile=?, " +" update_user=?,update_date=current_date(),isdel=? " +" where id=? ";PreparedStatement ptmt=conn.prepareStatement(sql);ptmt.setString(1, g.getUser_name());ptmt.setInt(2, g.getSex());ptmt.setInt(3, g.getAge());ptmt.setDate(4, new Birthday().getTime()));ptmt.setString(5, g.getEmail());ptmt.setString(6, g.getMobile());ptmt.setString(7, g.getUpdate_user());ptmt.setInt(8, g.getIsdel());ptmt.setInt(9, g.getId());ute();}public void delGoddess(Integer id) throws SQLException{Connection connConnection();String sql="" +" delete from imooc_goddess " +" where id=? ";PreparedStatement ptmt=conn.prepareStatement(sql);ptmt.setInt(1, id);ute();}public List<Goddess> query() throws Exception{List<Goddess> result=new ArrayList<Goddess>();Connection connConnection();StringBuilder sb=new StringBuilder();sb.append("select id,user_name,age from imooc_goddess ");PreparedStatement ptmt=conn.String());ResultSet rsuteQuery();Goddess g=null;()){g=new Goddess();g.Int("id"));g.setUser_String("user_name"));g.Int("age"));result.add(g);}return result;}public List<Goddess> query(String name,String mobile,String email) throws Exception{List<Goddess> result=new ArrayList<Goddess>();Connection connConnection();StringBuilder sb=new StringBuilder();sb.append("select * from imooc_goddess ");sb.append(" where user_name like ? and mobile like ? and email like ?");PreparedStatement ptmt=conn.String());ptmt.setString(1, "%"+name+"%");ptmt.setString(2, "%"+mobile+"%");ptmt.setString(3, "%"+email+"%");System.out.String());ResultSet rsuteQuery();Goddess g=null;()){g=new Goddess();g.Int("id"));g.setUser_String("user_name"));g.Int("age"));g.Int("sex"));g.Date("birthday"));g.String("email"));g.String("mobile"));g.setCreate_Date("create_date"));g.setCreate_String("create_user"));g.setUpdate_Date("update_date"));g.setUpdate_String("update_user"));g.Int("isdel"));result.add(g);}return result;}public List<Goddess> query(List<Map<String, Object>> params) throws Exception{List<Goddess> result=new ArrayList<Goddess>();Connection connConnection();StringBuilder sb=new StringBuilder();sb.append("select * from imooc_goddess where 1=1 ");if(params!=null&¶ms.size()>0){for (int i = 0; i < params.size(); i++) {Map<String, Object> map(i);sb.append(" and "("name")+" "("rela")+" "("value")+" ");}}PreparedStatement ptmt=conn.String());System.out.String());ResultSet rsuteQuery();Goddess g=null;()){g=new Goddess();g.Int("id"));g.setUser_String("user_name"));g.Int("age"));g.Int("sex"));g.Date("birthday"));g.String("email"));g.String("mobile"));g.setCreate_Date("create_date"));g.setCreate_String("create_user"));g.setUpdate_Date("update_date"));g.setUpdate_String("update_user"));g.Int("isdel"));result.add(g);}return result;}public Goddess get(Integer id) throws SQLException{Goddess g=null;Connection connConnection();String sql="" +" select * from imooc_goddess " +" where id=? ";PreparedStatement ptmt=conn.prepareStatement(sql);ptmt.setInt(1, id);ResultSet rsuteQuery();()){g=new Goddess();g.Int("id"));g.setUser_String("user_name"));g.Int("age"));g.Int("sex"));g.Date("birthday"));g.String("email"));g.String("mobile"));g.setCreate_Date("create_date"));g.setCreate_String("create_user"));g.setUpdate_Date("update_date"));g.setUpdate_String("update_user"));g.Int("isdel"));}return g;}
public class GoddessAction {//添加女神 对应Viewpublic void add(Goddess goddess) throws Exception{GoddessDao dao=new GoddessDao();goddess.setSex(1);goddess.setCreate_user("ADMIN");goddess.setUpdate_user("ADMIN");goddess.setIsdel(0);dao.addGoddess(goddess);}//查询女神 "[GET/G]:查看某位女神的详细信息n"public Goddess get(Integer id) throws SQLException{GoddessDao dao=new GoddessDao();(id);}//更新女神 "[UPDATE/U]:更新女神信息n" +public void edit(Goddess goddess) throws Exception{GoddessDao dao=new GoddessDao();dao.updateGoddess(goddess);}//删除女神 "[DELETE/D]:删除女神信息n" +public void del(Integer id) throws SQLException{GoddessDao dao=new GoddessDao();dao.delGoddess(id);}//public List<Goddess> query() throws Exception{GoddessDao dao=new GoddessDao();return dao.query();}//"[QUERY/Q]:查看全部女神的信息n" +public List<Goddess> query(List<Map<String, Object>> params) throws Exception{GoddessDao dao=new GoddessDao();return dao.query(params);}
public class View {private static final String CONTEXT="欢迎来到女神禁区:n" +"下面是女神禁区的功能列表:n" +"[MAIN/M]:主菜单n" +"[QUERY/Q]:查看全部女神的信息n" +"[GET/G]:查看某位女神的详细信息n" +"[ADD/A]:添加女神信息n" +"[UPDATE/U]:更新女神信息n" +"[DELETE/D]:删除女神信息n" +"[SEARCH/S]:查询女神信息(根据姓名、手机号来查询)n" +"[EXIT/E]:退出女神禁区n" +"[BREAK/B]:退出当前功能,返回主菜单";private static final String OPERATION_MAIN="MAIN";private static final String OPERATION_QUERY="QUERY";private static final String OPERATION_GET="GET";private static final String OPERATION_ADD="ADD";private static final String OPERATION_UPDATE="UPDATE";private static final String OPERATION_DELETE="DELETE";private static final String OPERATION_SEARCH="SEARCH";private static final String OPERATION_EXIT="EXIT";private static final String OPERATION_BREAK="BREAK";public static void main(String[] args) {System.out.println(CONTEXT);//怎么保持程序一直运行Scanner scan=new Scanner(System.in);Goddess goddess=new Goddess();GoddessAction action=new GoddessAction();String prenious=null;Integer step=1;while(scan.hasNext()){String in().toString();if(OPERATION_EXIT.UpperCase())||OPERATION_EXIT.substring(0, 1).UpperCase())){System.out.println("您已成功退出女神禁区。");break;}else if(OPERATION_QUERY.UpperCase())||OPERATION_QUERY.substring(0, 1).UpperCase())){try {List<Goddess> list=action.query();for (Goddess go : list) {System.out.Id()+",姓名:"User_name());}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}else if(OPERATION_ADD.UpperCase())||OPERATION_ADD.substring(0, 1).UpperCase())||OPERATION_ADD.equals(prenious)){prenious=OPERATION_ADD;//新增女神if(1==step){System.out.println("请输入女神的[姓名]");}else if(2==step){goddess.setUser_name(in);System.out.println("请输入女神的[年龄]");}else if(3==step){goddess.setAge(Integer.valueOf(in));System.out.println("请输入女神的[生日],格式如:yyyy-MM-dd");}else if(4==step){SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");Date birthday=null;try {birthday = sf.parse(in);goddess.setBirthday(birthday);System.out.println("请输入女神的[邮箱]");} catch (ParseException e) {e.printStackTrace();System.out.println("您输入的格式有误,请重新输入");step=3;}}else if(5==step){goddess.setEmail(in);System.out.println("请输入女神的[手机号]");}else if(6==step){goddess.setMobile(in);try {action.add(goddess);System.out.println("新增女神成功");} catch (Exception e) {e.printStackTrace();System.out.println("新增女神失败");}}if(OPERATION_ADD.equals(prenious)){step++; }}else{System.out.println("您输入的值为:"+in); }}}
public class DBUtil {private static final String URL="jdbc:mysql://127.0.0.1:3306/nsjq?useUnicode=true&characterEncoding=utf-8";private static final String USER="root";private static final String PASSWORD="lvc159357";private static Connection conn=null;static {try {//1.加载驱动程序Class.forName(sql.jdbc.Driver");//2.获得数据库的连接connConnection(URL, USER, PASSWORD);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}}public static Connection getConnection(){return conn;}
我应该知道的:
1.从控制台输入:
Scanner scan=new Scanner(System.in);
2.打通数据库:
Class.forName(sql.jdbc.Driver"); //加载驱动,从外部导入Msql包
Connection(URL, USER, PASSWORD); //连接数据库
对数据的操作:
String sql=“”;// sql语句
PreparedStatement ptmt=conn.prepareStatement(sql);//或者 Statement(sql);
------------------------------------------------------------------------------------------------------------------
3.使用了泛型,不会。
--------------------------------------------------------
整个项目逻辑的实现,业务的需求。处理好各个类的关系以及实现。
本文发布于:2024-02-04 14:49:33,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170709705856515.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |