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

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

    package test;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Scanner;
    
    /*编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。*/
    public class test {
    
        public static void main(String[] args) {
            Scanner reader = new Scanner(System.in);
            System.out.print("请输入用户名和密码:
    ");
            String username = reader.nextLine();
            String password = reader.nextLine();
            Connection con = null;
            PreparedStatement ps = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
                ps = con.prepareStatement("select * from t_login  where username=? and password=?");
                ps.setString(1, username);
                ps.setString(2, password);
                ResultSet rs = ps.executeQuery();
                if (rs.next()) {
                    System.out.print("yes");
                } else {
                    System.out.print("no");
                }
                if (rs != null) {
                    rs.close();
                }
    
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    ps.close();
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    二.成功界面截图

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

     1.DB.java

    package test1;
    
    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 db;
    
        public static DB getInstance() {
            if (db == null) {
                db = new DB();
            }
            return db;
        }
    
        private DB() {
            try {
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        static {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    
        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) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        }

    2.user.java

    package test1;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Scanner;
    /*题目2:
     * 在上一题基础上,当登录成功后,将t_user表(id、name、sex、birthday)的信息进行显示
     *(要求使用DB.java完成登录和获取t_user表中数据的操作),
     *最后再对t_user表进行一条记录的添加操作。*/
    public class user {
        public static void main(String[] args) {
            DB db=DB.getInstance();
            Scanner reader = new Scanner(System.in);
            System.out.print("请输入用户名和密码:
    ");
            String username = reader.nextLine();
            String password = reader.nextLine();
            ResultSet rs = db.executeselect("select * from t_login  where username=? and password=?", new Object[] {username,password});
            try {
                if (rs.next()) {
                    System.out.print("登陆成功!
    " + "t_user表的内容是
    ");
                    ResultSet rs1 = db.executeselect("select*from t_user",new Object[]{});
                    while (rs1.next()) {
                        String name = rs1.getString(2);
                        String sex = rs1.getString(3);
                        String birthday = rs1.getString(4);
                        System.out.println("name:" + name);
                        System.out.println("sex:" + sex);
                        System.out.println("birthday:" + birthday);
                    }
                    System.out.print("请输入您的name,sex,birthday
    ");
                    String name = reader.nextLine();
                    String sex = reader.nextLine();
                    String birthday = reader.nextLine();
                    int count=db.executeModify("insert into t_user(name,sex,birthday)values(?,?,?)", new Object[]{name,sex,birthday});
                        if (count > 0) {
                            System.out.print("更新成功");
                        } else {
                            System.out.print("更新失败");
                        }
                    }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            db.close();
        }
    }

    二.运行成功截图

  • 相关阅读:
    jQuery源码笔记——四
    jQuery源码笔记——三
    jQuery源码笔记——二
    深度理解作用域链和闭包
    事务的传播机制
    Jvm的运行时数据区
    SpringBoot 工程结构
    MyBatis的<if>标签判空
    Redis学习
    MyBatis调用Oracle的存储过程
  • 原文地址:https://www.cnblogs.com/912760869-qq/p/12023078.html
Copyright © 2011-2022 走看看