zoukankan      html  css  js  c++  java
  • 第15周作业

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

    package aa;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Scanner;
    
    public class Test {
    
        /**
         * 编写一个应用程序,输入用户名和密码,访问test数据库中t_login表
         * (字段包括id、username、password),验证登录是否成功
         */
        public static void main(String[] args) {
            Scanner input =new Scanner(System.in);
            String username=input.nextLine();
            String password=input.nextLine();
            Connection con=null;
            PreparedStatement pre=null;
            
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
    
            try {
                con=DriverManager.getConnection("jdbc:mysql://localhost:3306/aa", "root","1234" );
            } catch (SQLException e) {
            
                e.printStackTrace();
            }
            try {
                pre=con.prepareStatement("select*from bb where username=? and password=?");
                pre.setString(1,username);
                pre.setString(2,password);
                ResultSet res=pre.executeQuery();
                if(res.next()){
                    System.out.println("YES");
                }else{
                    System.out.println("no");
                }
                if(res!=null){
                    res.close();
                }
                
            } catch (SQLException e) {
                
                e.printStackTrace();
            }
            try {
                pre.close();
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }

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

    package aa;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Scanner;
    
    public class Test1 {
    
        /**
         在上一题基础上,当登录成功后,
         将t_user表(id、name、sex、birthday)的信息进行显示
         (要求使用DB.java完成登录和获取t_user表中数据的操作),
         最后再对t_user表进行一条记录的添加操作。
         */
        public static void main(String[] args) {
            Scanner input=new Scanner(System.in);
            System.out.println("请输入姓名和密码");
            String username=input.nextLine();
            String password=input.nextLine();
            Connection con=null;
            PreparedStatement pre=null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                
                e.printStackTrace();
            }
            
            try {
                con=DriverManager.getConnection("jdbc:mysql://localhost:3306/aa","root","1234");
                pre=con.prepareStatement("select*from bb where username=? and password=?");
                pre.setString(1, username);
                pre.setString(2, password);
                ResultSet res=pre.executeQuery();
                if(res.next()){
                     System.out.print("登陆成功!
    " + "t_user表的内容是
    ");
                        pre = con.prepareStatement("select*from user");
                        res = pre.executeQuery();
                        while (res.next()) {
                     String name = res.getString(2);
                     String sex = res.getString(3);
                     String birthday = res.getString(4);
                     System.out.println("name:" + res.getString("name"));
                     System.out.println("sex:" + res.getString("sex"));
                     System.out.println("birthday:" + res.getString("birthday"));
                     }
                  }
                System.out.println("请输入name,sex,birthday
    ");
                String name =input.nextLine();
                String sex = input.nextLine();
                String birthday = input.nextLine();
                pre = con.prepareStatement("insert into user(name,sex,birthday) values(?,?,?)");
                pre.setString(1, name);
                pre.setString(2, sex);
                pre.setString(3, birthday);
                int count=pre.executeUpdate();
                if(count>0){
                    System.out.println("数据更新成功");
                }
                else{
                    System.out.println("数据更新失败");
                }if(res!=null){
                    res.close();
                }
               
                
                
            } catch (SQLException e) {
                
                e.printStackTrace();
            } finally{
                try {
                    pre.close();
                    con.close();
                    
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            
            
            
        }
    
    }

     

  • 相关阅读:
    Java网络技术-待续
    Java输入输出技术
    Java数据库技术
    Java安全技术
    Java异常、事件、多线程
    网站产品设计
    C#-委派和事件
    Quartz 触发器(SimpleTrigger&CronTrigger )配置说明 & cronExpression表达式 转
    weblogic出现response already committed(转)
    Weblogic二种修改端口的方法(转)
  • 原文地址:https://www.cnblogs.com/chen4635/p/12046302.html
Copyright © 2011-2022 走看看