JAVA第15周作业

一、题目

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

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

二、代码

(1)

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();
            }
        }
    }

 (2) 

import java.sql.*;
import java.util.Scanner;

public class Testmain {
    public static void main(String[] args) {
        Scanner r = new Scanner(System.in);
        System.out.println("请输入用户名和密码:");
        String  username = r.next();
        String password = r.next();
        Connection con = null;
        Statement st = null;
        ResultSet rs=null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            try {
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                st = con.createStatement();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            String sql ="insert into t_login(username,password) values('" +username+ "','" + password + "')";

            int count = 0;
            try {
                count = st.executeUpdate(sql);
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } 
            if(count>0) {
                System.out.println("登陆成功");
                rs= DB.getInstance().executeSelect("select * from t_user", args);   //调用方法,查询表中信息

                try {
                    while(rs.next()) {  //循环输出
                        int id = rs.getInt(1);
                        String name = rs.getString(2);
                        int sex = rs.getInt(3);
                        String birthday = rs.getString(4);
                        System.out.println("id\t"+"name\t"+"sex\t"+"birthday");
                        System.out.println(id + "\t" + name + "\t" + sex + "\t" + birthday);
                    }
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                
                System.out.println("请输入id,name,sex,birthday:");//增加一条记录
                String id=r.next();
                String name=r.next();
                Integer sex = Integer.valueOf(r.next());
                String birthday = r.next();
                String sql1="insert into t_user(id,name,sex,birthday) values('"+id+"','"+name+"','"+sex+"','"+birthday+"')";
                int Count=DB.getInstance().executeModify(sql1, args);
                if(Count>0)
                    System.out.println("添加成功");
                else
                    System.out.println("添加失败,填写有误");
            }
            else
                System.out.println("登陆失败");


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                st.close();
                con.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}

  

三、运行截图

 

 

 

 

posted on 2019-12-15 22:21  心灵老鸭汤  阅读(292)  评论(0编辑  收藏  举报