| 
| 本帖最后由 张_涛 于 2012-7-22 15:11 编辑 
 主要方法代码,其他的参见附件。复制代码public class Operation {
        private Connection conn = null;
        String sql = null;
        private PreparedStatement pstmt = null;
        public Operation(Connection conn) {
                this.conn = conn;
        }
        
        public boolean doLogin() throws Exception{
                System.err.println("请输入:姓名,联系电话,并以','分开");
                
                Scanner scan = new Scanner(System.in);
                String infos = scan.next();
                String[] info = infos.split(",");
                String name = info[0];
                int tel = Integer.parseInt(info[1]);
                
                sql = "select name,tel,state from stu";
                //实例化数据库操作对象
                pstmt = this.conn.prepareStatement(sql);
                ResultSet rs = pstmt.executeQuery();
                while(rs.next()) {
                        //跟数据库中记录进行比较,判断姓名,联系电话是否匹配存在。
                        if(name.equals(rs.getString(1)) && tel == rs.getInt(2)) {
                                System.out.println("登录成功");
                                if(rs.getString(3).equals("yes")) {
                                        System.out.println("你已经通过所有考试,请按时到黑马训练营报道");
                                }
                                else {
                                        System.out.println("你还没有通过所有考试,请继续努力,争取早日加入黑马训练营!");
                                }
                                return true;
                        }
                }
                
                System.out.println("登录失败,请检查输入是否正确!");
                
                return false;
        }
        
        public void doCreate() throws Exception{
                Stu stu = new Stu();
                
                System.out.println("请输入:姓名,联系电话,入学状态,并以英文逗号','分开");
                Scanner scan = new Scanner(System.in);
                
                String infos = scan.next();
                String[] info = infos.split(",");
                stu.setName(info[0]);
                stu.setTel(Integer.parseInt(info[1]));
                stu.setState(info[2]);
                
                sql = "insert into stu(name,tel,state) values(?,?,?)";
                this.pstmt = this.conn.prepareStatement(sql);
                //设置查询所需要的内容
                this.pstmt.setString(1, stu.getName());
                this.pstmt.setInt(2, stu.getTel());
                this.pstmt.setString(3, stu.getState());
                if(this.pstmt.executeUpdate() > 0) {
                        System.out.println("注册成功!");
                }
                else {
                        System.out.println("注册失败!");
                }
                this.pstmt.close();
        }
        
        public void doUpdate() throws Exception {
                Stu stu = new Stu();
                
                System.out.println("请输入:更新的UID号,更新后的姓名,更新后的电话,更新后的状态,并以英文逗号','分开");
                Scanner scan = new Scanner(System.in);
                
                String infos = scan.next();
                String[] info = infos.split(",");
                
                stu.setUid(Integer.parseInt(info[0]));
                stu.setName(info[1]);
                stu.setTel(Integer.parseInt(info[2]));
                stu.setState(info[3]);
                
                sql = "update stu set name=?, tel=?, state=? where uid = ?";
                this.pstmt = this.conn.prepareStatement(sql);
                this.pstmt.setString(1, stu.getName());
                this.pstmt.setInt(2, stu.getTel());
                this.pstmt.setString(3, stu.getState());
                this.pstmt.setInt(4, stu.getUid());
                if(this.pstmt.executeUpdate() > 0) {
                        System.out.println("更新成功!");
                }
                else {
                        System.out.println("更新失败!");
                }
                this.pstmt.close();
        }
        
        public void doDelete() throws Exception {
                System.out.println("请输入需要删除的学生的UID:");
                
                Scanner scan = new Scanner(System.in);
                int uid = scan.nextInt();
                sql = "delete from stu where uid = ?";
                this.pstmt = this.conn.prepareStatement(sql);
                this.pstmt.setInt(1, uid);
                if(this.pstmt.executeUpdate() > 0) {
                        System.out.println("删除陈功!");
                }
                else {
                        System.out.println("删除失败!");
                }
                this.pstmt.close();
        }
        
        public void doBackup() throws Exception {
                System.out.println("------正在进行备份------");
                //创建一个XML文件,将数据备份到其中。
                File file = new File("data//stuback.xml");
                sql = "select uid,name,tel,state from stu";
                pstmt = this.conn.prepareStatement(sql);
                
                //声明一个Document对象
                Document doc = new Document();
                Element root = new Element("stus");
                ResultSet rs = pstmt.executeQuery();
                
                while(rs.next()) {
                        //定义各个节点
                        Element stu = new Element("stu");
                        Element uid = new Element("uid");
                        Element name = new Element("name");
                        Element tel = new Element("tel");
                        Element state = new Element("state");
                        
                        uid.setText(rs.getInt(1) + "");
                        name.setText(rs.getString(2));
                        tel.setText(rs.getInt(3) + "");
                        state.setText(rs.getString(4));
                        
                        //设置节点之间的关系
                        stu.addContent(uid);
                        stu.addContent(name);
                        stu.addContent(tel);
                        stu.addContent(state);
                        root.addContent(stu);
                }
                //关闭查询对象
                rs.close();
                //关闭操作对象
                pstmt.close();
                doc.addContent(root);
                //用来输出XML文件
                XMLOutputter out = new XMLOutputter();
                out.output(doc, new FileOutputStream(file));
                System.out.println("------备份成功------");
        }
        
        public void doReduction() throws Exception{
                System.out.println("------正在进行还原------");
                
                File file = new File("data//stuback.xml");
                //建立SAX解析
                SAXBuilder builder = new SAXBuilder();
                Document doc = builder.build(file);
                //读到根元素
                Element root = doc.getRootElement();
                //读取全部stu子元素
                List allStu = root.getChildren("stu");
                Iterator iter = allStu.iterator();
                
                Stu s = new Stu();
                sql = "delete from stu";
                pstmt = this.conn.prepareStatement(sql);
                pstmt.execute();
                sql = "insert into stu(uid,name,tel,state) values(?,?,?,?)";
                pstmt = this.conn.prepareStatement(sql);
                
                while(iter.hasNext()) {
                        Element stu = (Element)iter.next();
                        s.setUid(Integer.parseInt(stu.getChildText("uid")));
                        s.setName(stu.getChildText("name"));
                        s.setTel(Integer.parseInt(stu.getChildText("tel")));
                        s.setState(stu.getChildText("state"));
                        
                        pstmt.setInt(1, s.getUid());
                        pstmt.setString(2, s.getName());
                        pstmt.setInt(3, s.getTel());
                        pstmt.setString(4, s.getState());
                        pstmt.addBatch();
                }
                pstmt.executeBatch();
                System.out.println("------还原成功------");
        }
}
 | 
 |