测试JDBCUtils的重用性
2024-10-08 03:28:33
package cn.itcast.jdbc;
import cn.itcast.util.JDBCUtils;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;
/**
* @author newcityman
* @date 2019/8/15 - 21:28
* 1、 通过键盘录入用户名和密码
* 2、 判断用户是否登录成功
*/
public class JDBCDemo08 {
public static void main(String[] args) {
System.out.println("请输入用户名:");
Scanner sc = new Scanner(System.in);
String username = sc.next();
System.out.println("请输入密码");
String password = sc.next();
/* boolean flag = new JDBCDemo08().login(username, password);
if (flag) {
System.out.println("登录成功");
} else {
System.out.println("用户名或密码错误,请联系系统管理员");
}*/
boolean f = new JDBCDemo08().login2(username, password);
if (f) {
System.out.println("登录成功");
} else {
System.out.println("用户名或密码错误,请联系系统管理员");
}
}
/*
*登录方法
*
* */
public boolean login(String username, String password) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
if (username == null || password == null) {
return false;
}
try {
// 1、 获取连接
conn = JDBCUtils.getConnection();
// 2、 定义sql
String sql = "select * from user where name='" + username + "'and password='" + password + "'";
System.out.println(sql);
// 3、 获取执行sql的对象
stmt = conn.createStatement();
// 4、执行查询
rs = stmt.executeQuery(sql);
// 5、判断
/*if (rs.next()) {
System.out.println("登录成功");
return true;
} else {
System.out.println("用户名或密码错误,请重新输入");
return false;
}*/
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(rs, stmt, conn);
}
return false;
}
/*
*登录方法2:防止sql注入
*
* */
public boolean login2(String username, String password) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
if (username == null || password == null) {
return false;
}
try {
// 1、 获取连接
conn = JDBCUtils.getConnection();
// 2、 定义sql
String sql = "select * from user where name=? and password=?";
// 3、 获取执行sql的对象
stmt = conn.prepareStatement(sql);
stmt.setString(1,username);
stmt.setString(2,password);
// 4、执行查询
rs = stmt.executeQuery();
// 5、判断
/*if (rs.next()) {
System.out.println("登录成功");
return true;
} else {
System.out.println("用户名或密码错误,请重新输入");
return false;
}*/
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(rs, stmt, conn);
}
return false;
}
}
最新文章
- [Java 基础]数据类型
- mysql5.6新特性总结
- JDBC学习笔记(2)
- 深入理解docker的link机制
- js Array对象
- ASP.NET中Web DataGrid的使用指南
- 5.1 Intent
- Laxcus大数据管理系统2.0(2)- 第一章 基础概述 1.1 基于现状的一些思考
- Sublime_调试PHP编译系统设置.
- eclipse打开时提示:failed to create the java virtual machine
- blog建表操作
- sprintf、strcpy 及 memcpy 函数区别
- JavaScript之深入理解【函数】
- Genymotion安卓模拟器和VirtualBox虚拟机安装、配置、测试
- SpringBoot------自动装配Mapper报错
- HW2017笔试编程题
- js Object.prototype.toString.call()
- 关于Java Logger类的使用问题 - 内存不释放
- qt cmake
- UDP与TCP笔记
热门文章
- macos proxy_bypass_macosx_sysconf exception
- 第三周PTA笔记 回文数+A-B(大数减法)+高精度除法+数楼梯(大数加法)
- 问题 C: A+B Problem II
- Django笔记&教程 6-2 表单(Form)基础操作
- jenkins内置变量的使用
- 力扣 - 剑指 Offer 22. 链表中倒数第k个节点
- TCP、三次握手、四次挥手(图解)
- freeswitch APR库哈希表
- Redis 很屌,不懂使用规范就糟蹋了
- DirectX12 3D 游戏开发与实战第八章内容(上)