小小登陆程序

本文提供了一个使用 Java 和 MySQL 实现简单登录验证的示例程序。通过该程序,用户可以输入用户名和密码,系统将从数据库中验证这些信息,并给出登录成功或失败的反馈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Code:
  1. import  java.util.*;  
  2. import  java.sql.*;  
  3. public   class  DBtest {  
  4.   
  5.     /**  
  6.      * @param args  
  7.      */   
  8.     public   static   void  main(String[] args) {  
  9.         // TODO Auto-generated method stub   
  10.           
  11.         String driver = "com.mysql.jdbc.Driver" ;  
  12.         //String url = "jdbc:mysql://localhost/javadb";   
  13.         String user = "root" ;  
  14.         String pd = "123456" ;  
  15.         try {  
  16.             Class.forName("com.mysql.jdbc.Driver" );  
  17.             String url="jdbc:mysql://localhost/javadb?user=root&password=123456" ;  
  18.             Connection conn = DriverManager.getConnection(url);  
  19.             //Connection conn = DriverManager.getConnection(url,user,pd);   
  20.             Statement stmt = conn.createStatement();  
  21.             String name;  
  22.             String password;  
  23.             System.out.print("请输入用户名:" );  
  24.             Scanner sc= new  Scanner(System.in);  
  25.             name = sc.next();  
  26.             System.out.print("请输入密码:" );  
  27.             password = sc.next();  
  28.             String sql = "select count(*) from admin where username = '" +name+ "' and passwd = '" +password+ "'" ;  
  29.             ResultSet rs = stmt.executeQuery(sql);  
  30.               
  31.             rs.next();  
  32.             int  count = rs.getInt( 1 );  
  33.             if (count >  0  )  
  34.                 System.out.println("登陆成功!" );  
  35.             else   
  36.                 System.out.println("登陆失败!" );  
  37.             rs.close();  
  38.             stmt.close();  
  39.             conn.close();  
  40.         }catch (Exception e){  
  41.             System.out.print("程序错误!" );  
  42.         }  
  43.           
  44.     }  
  45.   
  46. }  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值