本文实例为大家分享了jdbc工具类实现登录功能的具体代码,供大家参考,具体内容如下
我们使用jdbc实现数据库的增删改查,代码基本差不多,有很多重复,所以我们可以把这些重复的代码写成一个工具类,使用的时候直接调用就可以了。下面以实现登录功能的案例来介绍。
创建数据库,插入数据
1
2
3
4
5
6
7
8
9
|
use student;
create table user(
id int primary key auto_increment,
username varchar( 32 ),
password varchar( 32 )
);
insert into user values( null , 'zhangsan' , '123' );
insert into user values( null , 'lisi' , '234' );
|
jdbc.properties
1
2
3
4
|
url=jdbc:mysql: //localhost:3306/student
username=root
password=root
driver=com.mysql.jdbc.driver
|
jdbc工具类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package cn.itcast.util;
import java.io.filereader;
import java.io.ioexception;
import java.net.url;
import java.sql.*;
import java.util.properties;
/**
* jdbc工具类
**/
public class jdbcutils {
private static string url;
private static string username;
private static string password;
private static string driver;
/**
* 文件的读取,著需要读取一次即可拿到这些值,使用静态代码块
**/
static {
try {
//1、读取资源文件,获取值
properties properties = new properties();
//获取src路径下的文件的方式 ---> classloader类加载器
classloader classloader = jdbcutils. class .getclassloader();
url resource = classloader.getresource( "jdbc.properties" );
string path = resource.getpath();
//2、加载文件
properties.load( new filereader(path));
//3、获取数据,赋值
url = properties.getproperty( "url" );
username = properties.getproperty( "username" );
password = properties.getproperty( "password" );
driver = properties.getproperty( "driver" );
//4、注册驱动
class .forname(driver);
} catch (ioexception e) {
e.printstacktrace();
} catch (classnotfoundexception e) {
e.printstacktrace();
}
}
/**
* 获取连接
* @return 连接对象
*/
public static connection getconnection() throws sqlexception {
return drivermanager.getconnection(url,username,password);
}
/**
* 释放资源
* @param statement
* @param connection
*/
public static void close(statement statement,connection connection) {
if (statement != null ) {
try {
statement.close();
} catch (sqlexception e) {
e.printstacktrace();
}
}
if (connection != null ) {
try {
connection.close();
} catch (sqlexception e) {
e.printstacktrace();
}
}
}
/**
* 释放资源
* @param resultset
* @param statement
* @param connection
*/
public static void close(resultset resultset,statement statement, connection connection) {
if (resultset != null ) {
try {
resultset.close();
} catch (sqlexception e) {
e.printstacktrace();
}
}
if (statement != null ) {
try {
statement.close();
} catch (sqlexception e) {
e.printstacktrace();
}
}
if (connection != null ) {
try {
connection.close();
} catch (sqlexception e) {
e.printstacktrace();
}
}
}
}
|
实现登录功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
package cn.itcast.jdbc;
import cn.itcast.util.jdbcutils;
import java.sql.*;
import java.util.scanner;
/**
* 1、通过键盘录入用户名和密码
* 2、判断用户是否登录成功
*/
public class jdbclogin {
public static void main(string[] args) {
//1、键盘录入,接收用户和密码
scanner sc = new scanner(system.in);
system.out.println( "请输入用户名:" );
string username = sc.nextline();
system.out.println( "请输入密码:" );
string password = sc.nextline();
//2、调用方法
boolean flag = new jdbclogin().login(username,password);
//3、判断
if (flag) {
system.out.println( "登录成功" );
} else {
system.out.println( "用户名或密码错误!" );
}
}
/**
* 登录方法
*/
public boolean login(string username,string password) {
if (username == null || password == null ) {
return false ;
}
//连接数据库判断是否登陆成功
connection connection = null ;
preparedstatement preparedstatement = null ;
resultset resultset = null ;
try {
//1、获取链接
connection = jdbcutils.getconnection();
//2、定义sql
string sql = "select * from user where username = ? and password = ?" ;
//3、获取执行sql的对象
//为了防止sql注入,实现事务安全,效率更高,必须要用preparedstatement
preparedstatement = connection.preparestatement(sql);
//给?赋值
preparedstatement.setstring( 1 ,username);
preparedstatement.setstring( 2 ,password);
//4、执行查询,不需要传递sql
resultset = preparedstatement.executequery();
//5、判断:如果是下一行,则返回true
return resultset.next();
} catch (sqlexception e) {
e.printstacktrace();
} finally {
jdbcutils.close(resultset,preparedstatement,connection);
}
return false ;
}
}
|
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_44668898/article/details/107412660