zoukankan      html  css  js  c++  java
  • 第15周作业

    题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。

    题目2:在上一题基础上,当登录成功后,将t_user表(id、name、sex、birthday)的信息进行显示(要求使用DB.java完成登录和获取t_user表中数据的操作),最后再对t_user表进行一条记录的添加操作。

    代码:

    封装DB:

    package demo;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class DB{
        private Connection con;
        private PreparedStatement pre;
        private ResultSet rs;
        private static DB test;
        
        static{
            try{
            Class.forName("com.mysql.jdbc.Driver");
            }catch(ClassNotFoundException e){
                e.printStackTrace();
            }
        }
        
        DB(){
            try{
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","980610");
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
        
        public static DB getInstabce(){
            if(test==null){
                test=new DB();
            }
            return test;
        }
        
        public ResultSet executeSelect(String sql,Object[] args){
            try{
                pre=con.prepareStatement(sql);
                if(args.length!=0){
                    for(int i=0;i<args.length;i++){
                        pre.setObject(i+1, args[i]);
                    }
                }
                rs = pre.executeQuery();
            }catch(SQLException e){
                e.printStackTrace();
            }
            return rs;
        }
        
        public int executeModify(String sql,Object[] args){
            int n=0;
            try{
                pre=con.prepareStatement(sql);
                if(args.length!=0){
                    for(int i=0;i<args.length;i++){
                        pre.setObject(i+1, args[i]);
                    }
                }
                n = pre.executeUpdate();
            }catch(SQLException e){
                e.printStackTrace();
            }
            return n;
        }
        
        public void close(){
            try{
                if(rs!=null){
                    rs.close();
                }
                pre.close();
                con.close();
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
    }

    TestMain类:

    package demo;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Scanner;
    import demo.DB;
    
    public class TestMain {
        public static void main(String[] args) {
            Scanner input=new Scanner(System.in);
            String username=input.next();
            String password=input.next();
            ResultSet a;        
            ResultSet b;        
            DB db = new DB();    
            a = db.executeSelect("select * from t_login where username = '"+username+"' and password = '"+password+"'", args);
            try {
                if (a.next()) {
                    System.out.println("登录成功");
                    System.out.println("以下为t_user表中的信息:");
                    b = db.executeSelect("select * from t_user", args);
                    while(b.next()) {
                        int id = b.getInt(1);
                        String name = b.getString(2);
                        int sex = b.getInt(3);
                        String birthday = b.getString(4);
                        System.out.println("id:"+id+"	name:"+name+"	sex:"+sex+"	birthday:"+birthday);    
                    }
                    System.out.println("请输入插入信息:");
                    int id1 = input.nextInt();
                    String name1 = input.next();
                    int sex1 = input.nextInt();
                    String date1 = input.next();
                    String sql="insert into t_user(id,name,sex,birthday) values("+id1+",'"+name1+"',"+sex1+",'"+date1+"')";
                    int n = db.executeModify(sql, args);    
                    if(n>0) {
                        System.out.println("插入成功");
                    }else {
                        System.out.println("插入失败");
                    }
                }else {
                    System.out.println("登陆失败");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
    }

    运行结果:

    t_login初始表:

    t_user初始表:

    运行:

    插入信息后的t_user表:

  • 相关阅读:
    python+matplotlib制作雷达图3例分析和pandas读取csv操作
    python+pygame的导弹追踪鼠标游戏设置和说明
    python+pygame制作一个可自定义的动态时钟和详解
    python+tkinter制作一个可自定义的动态时钟及详细解释,珍藏版
    教你如何用python和pygame制作一个简单的贪食蛇游戏,可自定义
    博客园的博客之美化日历
    博客园的博客美化之-增加首页音乐播放器APlayer
    博客园的博客美化之文章推荐和反对按钮、看板娘、图片放大、github链接、返回顶部小火箭
    设计模式之装饰器模式
    设计模式之状态模式
  • 原文地址:https://www.cnblogs.com/lz150520/p/12045428.html
Copyright © 2011-2022 走看看