第15周作业

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

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

代码:

DB.JAVA:

package llll;

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","");
        }catch(SQLException e) {
            e.printStackTrace();
        }
    }
    
    public static DB getInstance() {
        if(test == null) {
            test = new DB();
        }
        return test;
    }
    
    public ResultSet executeSelect(String sql, Object[] args) {
        ResultSet rs = null;
        try {
            pre = con.prepareStatement(sql);
            if(args.length !=0) {
                for(int i = 0;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) {
                    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();
    }
}
}

Text.JAVA:

package llll;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import llll.DB;

public class text{
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        String username=input.next();
        String password=input.next();
        ResultSet o;        
        ResultSet p;        
        DB db = new DB();    
        o = db.executeSelect("select * from t_login where username = '"+username+"' and password = '"+password+"'", args);
        try {
            if (o.next()) {
                System.out.println("登录成功");
                System.out.println("以下为t_user表中的信息:");
                p = db.executeSelect("select * from t_user", args);
                while(p.next()) {
                    int id = p.getInt(1);
                    String name = p.getString(2);
                    int sex = p.getInt(3);
                    String birthday = p.getString(4);
                    System.out.println("id:"+id+"\tname:"+name+"\tsex:"+sex+"\tbirthday:"+birthday);    
                }
                System.out.println("请输入插入信息:");
                int yy = input.nextInt();
                String qq = input.next();
                int tt = input.nextInt();
                String uu = input.next();
                String sql="insert into t_user(id,name,sex,birthday) values("+yy+",'"+qq+"',"+tt+",'"+uu+"')";
                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();
        }
    }

}

 

运行结果:

第15周作业_第1张图片第15周作业_第2张图片第15周作业_第3张图片

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