• Java第15周作业集



    一、题目1

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

    二、源代码

    package fifteen;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Scanner;
    public class Test1 {
        /**
         * @param args
         * @throws SQLException 
         */
        public static void main(String[] args) throws SQLException {
            // TODO Auto-generated method stub
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入用户名:");
            String username=sc.next();
            System.out.println("请输入密码:");
            String password=sc.next();
            try {
                Class.forName("com.mysql.jdbc.Driver");//加载驱动
                Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","1234");//数据路连接
                Statement st=con.createStatement();//创建执行sql语句对象
                ResultSet rs=st.executeQuery("select * from t_login where username='"+username+"'and password='"+password+"'");//sql语句
                if(rs.next()){
                    System.out.println("Yes");
                }else {
                    System.out.println("No");
                }
                if(rs!=null){
                    rs.close();
                }
                st.close();
                con.close();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
        }
    }

    三、运行结果

    一、题目2

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

    二、源代码

    DB.java

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

    Test2.java

    package fifteen;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Scanner;
    public class Test2 {
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            DB db=DB.getInstance();
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入用户名 密码:");
            String username=sc.next();
            String password=sc.next();
            ResultSet rs =null;
            //调用excuteSelect进行查询
            rs = db.excuteSelect("select * from t_login where username = ? and password = ?", new Object[]{username,password});
            try {
                if(rs.next()){
                    System.out.println("登录成功");
                    System.out.println("查询结果为:");
                    //调用excuteSelect进行查询
                    rs = db.excuteSelect("select * from t_user", new Object[]{});
                    char sex = ' ';
                    while(rs.next()){
                        if(rs.getInt(3)==0){
                            sex = '男';
                        }else if(rs.getInt(3)==1){
                            sex = '女';
                        }
                        System.out.println("id: "+rs.getInt(1)+"   name: "+rs.getString(2)+"   sex: "+sex+"   birthday: "+rs.getString(4));                 
                    }
                    System.out.println("添加如下信息");
                    System.out.println("姓名(name) 性别(sex) 生日(birthday)");
                    String name;
                    String birthday;
                    name=sc.next();
                    sex=sc.next().charAt(0);
                    birthday=sc.next();
                    int isex = 0;//初值默认为0
                    if(sex=='男'){
                        isex=0;
                    }else if(sex=='女'){
                        isex=1;
                    }//调用executeModify添加且判断
                    if(db.executeModify("insert into t_user (name,sex,birthday)values(?,?,?)", new Object[]{name,isex,birthday})>0){
                        System.out.println("添加成功");
                    }
                }else{
                    System.out.println("登录失败");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            db.close();//关闭数据库连接
            sc.close();
        }
    }

    三、运行结果

     

     

  • 相关阅读:
    asp.net留言板项目源代码下载
    HoverTree项目添加了查看留言列表功能
    HoverTree开源项目已经实现管理员登录
    HoverTree项目已经实现分层
    c# 连接Mysql数据库
    单行文字滚动就用myslider
    C#播放MP3源代码
    PHP 判断是否为 AJAX 请求
    c# TCP Socket通讯基础
    javascript类型注意事项
  • 原文地址:https://www.cnblogs.com/jingxueyan/p/12037862.html
Copyright © 2020-2023  润新知