• 第15周作业


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

    代码部分:三个对象

     1 /**
     2  * 声明Connection类的对象con,用来链接数据库
     3  * 声明PreparedStatement类的对象ps,执行预处理SQL语句
     4  * 声明ResultSet类的对象rt,接收结果集
     5  */
     6 
     7 package cn.edu.ccut;
     8 import java.sql.*;
     9 import java.util.*;
    10 public class TestMain {
    11 
    12     public static void main(String[] args) {
    13         Scanner der=new Scanner(System.in);
    14         System.out.println("请输入账号");
    15         String username =der.nextLine();
    16         System.out.println("请输入密码");
    17         String password =der.nextLine();
    18         
    19         Connection con=null;//链接数据库
    20         PreparedStatement ps=null;//声明预处理对象
    21         ResultSet rs=null;//接收查询结果
    22         try {
    23                 Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
    24                 String st1="jdbc:mysql://localhost:3306/db?useSSL=false";//数据库的网络地址://本地主机:端口号/数据库名字?关闭useSSL
    25                 con=DriverManager.getConnection(
    26                         st1,"root","Msunshuaiqun12.3");//数据库链接地址,用户名,密码
    27                 
    28                 String sql ="select * from t_login where username =? and password=?";
    29                 ps=con.prepareStatement(sql);//创建预处理语句对象
    30                 ps.setString(1, username);
    31                 ps.setString(2, password);
    32                 rs=ps.executeQuery();        //创建结果集对象
    33 
    34                 if(rs.next()) {
    35 
    36                     System.out.println("验证登陆成功");
    37                   }else {
    38                       System.out.println("验证登陆失败");
    39                   }
    40                 if(rs !=null) {
    41                     rs.close();//关闭链接
    42                 }
    43                 ps.close();
    44                 con.close();
    45             
    46         } catch (ClassNotFoundException e) {
    47             e.printStackTrace();
    48         } catch (SQLException e) {
    49             e.printStackTrace();
    50         }    
    51     }
    52 }

    运行截图:

  • 相关阅读:
    深入浅出HTTP请求(转自http://www.cnblogs.com/yin-jingyu/archive/2011/08/01/2123548.html)
    IOS定位
    webView(简单的浏览器)
    Get&Post登录
    IOS多媒体
    IOS VFL屏幕自适应
    IOS中在自定义控件(非视图控制器)的视图跳转中 代理方法与代码块的比较
    单例设计的定义
    动画
    多线程
  • 原文地址:https://www.cnblogs.com/sunshuaiqun/p/12032067.html
Copyright © 2020-2023  润新知