• 第15周作业


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

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

    源代码:

    1.DB.java

    package demo;
    import java.sql.*;
    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) {
                    e.printStackTrace();
                }
            }
            public DB() {
                try {
                    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123");
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            public static DB getInstance() {
                if(db == null) {
                    db = new DB();
                }
                return db;
            }
            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.test.java

    package demo;
     
    import java.sql.*;
    
    import java.util.Scanner;
    
    public class test {
     
        public static void main(String[] args) {
          
            Scanner scanner=new Scanner(System.in);//创建一个输入对象
            String username=scanner.next();//控制台输入一个String类型的username
            String password=scanner.next();//控制台输入一个String类型的password
            Connection con=null;//定义数据库连接对象con
            Statement st=null;//定义SQL语句执行对象st
            ResultSet rs=null;//定义SQL语句执行对象rs
            ResultSet rs1=null;//定义SQL语句执行对象rs1
            try {
                DriverManager.registerDriver(new com.mysql.jdbc.Driver());
               
                con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123");
                //建立数据库的连接
                st=con.createStatement(); //创建SQL语句执行对象st
     
                 String sql = "select * from t_login where username='" + username+ "' and password = '" + password + "'";
               
                 rs=st.executeQuery(sql); //查询sql语句中的username和password
                 boolean bl=rs.next();
                 if(bl) {
                     System.out.println("登陆成功");
                    rs1= DB.getInstance().executeSelect("select * from t_user", args);//预处理
                    while(rs1.next()) {
                        int id = rs1.getInt(1);
                        String name = rs1.getString(2);
                        String sex = rs1.getString(3);
                        String birthday = rs1.getString(4);
                        System.out.println("id       name        sex      birthday");
                        System.out.println(id + "       " + name + "       " + sex + "      " + birthday);
                    }
                    System.out.println("添加信息");
                    int id=scanner.nextInt();
                    String name=scanner.next();
                    String sex=scanner.next();
                    String birthday=scanner.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 (SQLException e) {           
                e.printStackTrace();
            }finally {
                try {
                    st.close();
                } catch (SQLException e) {               
                    e.printStackTrace();
                }
                try {
                    con.close();
                } catch (SQLException e) {              
                    e.printStackTrace();
                }
            }
        }
     
    }

    运行结果:

     

  • 相关阅读:
    IE盒子模型和标准W3C盒子模型
    [转载] MVC3自定义标签,给Html.ActionLink加上支持图片链接的功能
    MVC3获取登录用户名
    [转]APS.netMVC的ViewModel问题
    [转载]Js小技巧||给input type=“password”的输入框赋默认值
    [资料]aspnetdb.mdf数据库的建立和使用
    [转] .net网页中插入Flash
    [转]Membership、MembershipUser和Roles类
    [小技巧]提交按钮
    asp.net mvc中session的使用 样例
  • 原文地址:https://www.cnblogs.com/GXTSTAY/p/12045284.html
Copyright © 2020-2023  润新知