第15周作业

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

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

DB类

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();
        }
    }
    
    private 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) {
        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类:

import java.util.Scanner;

import com.mysql.jdbc.PreparedStatement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestMain {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        String username = sc.next();
        String password = sc.next();
        Connection con = null;
        Statement st = null;
        ResultSet rs=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
            st = con.createStatement();
            String sql = "insert into t_login(username,password) value('" + username + "','" + password +"')";
            int count = st.executeUpdate(sql);
            if(count > 0) {
                System.out.println("登录成功");
            } else {
                System.out.println("登录失败,请重新登录");
            }
            rs=DB.getInstance().executeSelect("select * from t_user", args);            
            while(rs.next()){    
                System.out.println("name  "+rs.getString(2)+"\t"+"sex  "+rs.getString(3)+"\t"+"birthday  "+rs.getString(4)+"\n");    
                
            }
            System.out.println("请输入name");
            String name=sc.next();
            System.out.println("请输sex");
            String sex=sc.next();
            System.out.println("请输入birthday");
            String birthday=sc.next();
            String sql1="insert into t_user (birthday,sex,name)values('"+birthday+"','"+sex+"','"+name+"')";

            int i=DB.getInstance().executeModify(sql1, args);
            if(i > 0) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失败");
            }
        }  catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                st.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

运行截图:

第15周作业_第1张图片

 

 t_login表:

 

添加成功后 t_user表:

第15周作业_第2张图片

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