第十五周作业

一、题目

  编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。当登录成功后,将t_user表(id、name、sex、birthday)的信息进行显示(要求使用DB.java完成登录和获取t_user表中数据的操作),最后再对t_user表进行一条记录的添加操作。

二、代码

package ta;
import java.sql.*;
public class DB {
    private static DB db;
    private PreparedStatement pre;
    private ResultSet rs;
    private Connection con;
    public static DB getInstance(){
        if(db==null){
            db=new DB();
        }
        return db;
    }
    DB(){
        try {
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/_1","root","");
        } catch (SQLException e) {
    
            e.printStackTrace();
        }
                    
        }
    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            
            e.printStackTrace();
        }
    }
    ResultSet  getQuery(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) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rs;
    }
     int  getUpdate(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;
        }
        void close(){
            try {
                 if(rs != null){
                        rs.close();                
                    }
                    pre.close();
                    con.close(); 
            } catch (SQLException e) {
                    e.printStackTrace();
            }
            
        }
    }
package ta;
public class UserAdd {

    public void addUser(String name,String sex,String birthday) {
        
        String sqlAdd = "insert into t_user  (name,sex,birthday) value(?,?,?)";
        Object[] value ={name,sex,birthday} ;
        int n = DB.getInstance().getUpdate(sqlAdd,value);
        if (n > 0) {//判断是否修改了行数
            System.out.println("插入数据成功");
        } else {
            System.out.println("插入数据失败");
        }
        DB.getInstance().close();

    }
}
package ta;


    import java.sql.*;

    public class UserSelect {
        ResultSet rs;
        public void allUser() {

            String sqlQuery = "SELECT * FROM  t_user";
            Object[] value={};
            rs = DB.getInstance().getQuery(sqlQuery,value);
            try {
                System.out.println("序号        用户名         性别      生日");
                while (rs.next()) {
                   
                    int id = rs.getInt(1);
                    String name = rs.getString(2);
                    String sex = rs.getString(3);
                    String birthday = rs.getString(4);
                    System.out.println(id + "      " + name + "       "
                            + sex + "        " + birthday);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }
package ta;
import java.sql.*;
import java.util.*;

public class a {

 public static void main(String[] args) {
                Scanner reader = new Scanner(System.in);
                System.out.println("输入用户名:");
                String username = reader.next();
                System.out.println("输入密码:");
                String password = reader.next();
              
                ResultSet rs = null;
                try {
                    String sql = "select * from t_login where username=? and password = ?";
                    Object[] value={username,password};
                    rs = DB.getInstance().getQuery(sql,value);
                    boolean b = rs.next();
                    if (b) {
                        System.out.println("登录成功,进入系统管理");
                        System.out.println("1 用户增加  2 用户查询  3系统退出");
                        int n = reader.nextInt();
                        while(true){
                            switch (n) {
                            case 1: {
                                UserAdd adduser = new UserAdd();
                                System.out.println("用户名");  String  name = reader.next();
                                System.out.println("性别");    String  sex = reader.next();
                                System.out.println("生日");    String  birthday = reader.next();
                                adduser.addUser(name, sex,birthday);
                                break;
                            }
                            case 2:{
                                UserSelect a1 = new UserSelect();
                               a1.allUser();
                                break;
                            }
                            case 3:{
                                System.out.println("程序结束");System.exit(0);
                            }
                            }
                            System.out.println("1 用户增加  2 用户查询  3系统退出");
                            n = reader.nextInt();
                        }
                        
                        
                    } else {
                        System.out.println("登录失败");
                    }
                    DB.getInstance().close();//关闭流
                } catch (SQLException e) {
                    System.out.println("DB连接错误");
                    e.printStackTrace();
                }
            }
        
    }

运行:

第十五周作业_第1张图片

你可能感兴趣的:(第十五周作业)