JAVA 密码验证

import java.util.Scanner;
public class test8 {
public static void main(String[] args){
Scanner in=new Scanner(System.in);


while(1==1){
System.out.println("请输入密码:(六位数字)");
int i=in.nextInt();
if(i!=123456){
continue;            //continue 跳出重新循环执行
}else{
System.out.println("密码输入正确---请进入。。。。");
break;
}


}
}


}
package cn.edu.hbue.ghp; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class tainjia { public static void main(String[] args) { // TODO Auto-generated method stub java.sql.Connection connection = null; PreparedStatement preStmt = null; try{ Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/grade?user=root&"+"password=ghp"; connection = DriverManager.getConnection(url); if(connection != null){ System.out.println("连接成功"); } String sql = "insert into book(author,publisher,isbn,pubDate,price,categoryId)"+"values(?,?,?,?,?,?)"; preStmt = connection.prepareStatement(sql); preStmt.setString(1, "EDA技术"); preStmt.setString(2, "清华大学"); preStmt.setString(3, "1"); preStmt.setString(4, "2017-1-1"); preStmt.setDouble(5, 66); preStmt.setInt(6, 2); preStmt.executeUpdate(); preStmt.setString(1, "你要去相信,没有到不了的明天"); preStmt.setString(2, "湖南出版时"); preStmt.setString(3, "2"); preStmt.setString(4, "2014-11-13"); preStmt.setDouble(5, 32.8); preStmt.setInt(6, 1); preStmt.executeUpdate(); System.out.println("添加数据的行为数为"+preStmt.getUpdateCount()); }catch(ClassNotFoundException e){ System.out.println("JDBC driver error"); }catch(SQLException e){ System.out.println("JDBC operation error"); }finally{ try{ if(preStmt != null){ preStmt.close(); } }catch(SQLException e){ System.out.println("PreparedStatement close error"); } try{ if( connection != null){ connection.close(); } }catch(SQLException e){ System.out.println("PreparedStatement close error"); } } } }
判断用户名和密码是否正确,需要根据具体的业务逻辑来实现。一般来说,我们会将用户名和密码存储在数据库中,用户登录时,我们需要将用户输入的用户名和密码与数据库中的数据进行比对。 以下是一个简单的示例代码,用于判断输入的用户名和密码是否和数据库中的数据匹配: ```java public static boolean isMatch(String username, String password) { // 连接数据库 Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password"); // 查询数据库中是否存在该用户 String sql = "SELECT * FROM user WHERE username=? AND password=?"; stmt = conn.prepareStatement(sql); stmt.setString(1, username); stmt.setString(2, password); rs = stmt.executeQuery(); if (rs.next()) { // 用户名和密码匹配 return true; } else { // 用户名和密码不匹配 return false; } } catch (SQLException e) { e.printStackTrace(); return false; } finally { // 关闭数据库连接 try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } ``` 在这个方法中,我们首先连接数据库,然后查询数据库中是否存在与输入的用户名和密码匹配的数据。如果查询到了数据,就说明用户名和密码正确,返回true,否则返回false。需要注意的是,在使用完数据库连接后,需要将其关闭以释放资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值