552JDBC工具类和553JDBC练习登录案例
抽取JDBC工具类:JDBCutls
目的:简化书写
分析:
1.注册驱动也抽取
2.抽去一个方法获取连接对象
需求:不想传递参数(麻烦),还得保证工具类的通用性
解决:配置文件
jdbc.properties
url=
user=
password=
url=jdbc:mysql://localhost:3306/practice?useUnicode=true&characterEncoding=utf-8&useSSL=false
user=root
password=root
driver=com.mysql.jdbc.Driver
3.抽取一个方法释放资源
public class JDBCUtils {
private static String url;
private static String user;
private static String password;
private static String driver;
/**
* 文件的读取,只需要读取一次即可拿到这些值,使用静态代码块
*/
static {
//读取资源文件,获取值
try {
//1.创建Properties集合
Properties properties = new Properties();
//获取src路径下的文件的方式---》ClassLoader 类加载器
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL resource = classLoader.getResource("jdbc.properties");
String path = resource.getPath();
System.out.println(path);
//2.加载文件
properties.load(new FileReader(path));
//3.获取数据,赋值
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
driver = properties.getProperty("driver");
//4.注册驱动
Class.forName(driver);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
/**
* 获取连接
*
* @return 连接对象
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
/**
* 释放资源
*
* @param stmt
* @param conn
*/
public static void close(Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
/**
* 释放资源
*
* @param stmt
* @param conn
*/
public static void close(ResultSet re, Statement stmt, Connection conn) {
if (re != null) {
try {
re.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
案例
public class JDBCUtils {
private static String url;
private static String user;
private static String password;
private static String driver;
/**
* 文件的读取,只需要读取一次即可拿到这些值,使用静态代码块
*/
static {
//读取资源文件,获取值
try {
//1.创建Properties集合
Properties properties = new Properties();
//获取src路径下的文件的方式---》ClassLoader 类加载器
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL resource = classLoader.getResource("jdbc.properties");
String path = resource.getPath();
System.out.println(path);
//2.加载文件
properties.load(new FileReader(path));
//3.获取数据,赋值
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
driver = properties.getProperty("driver");
//4.注册驱动
Class.forName(driver);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
/**
* 获取连接
*
* @return 连接对象
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
/**
* 释放资源
*
* @param stmt
* @param conn
*/
public static void close(Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
/**
* 释放资源
*
* @param stmt
* @param conn
*/
public static void close(ResultSet re, Statement stmt, Connection conn) {
if (re != null) {
try {
re.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
JDBC练习登录案例
练习
需求:通过键盘录入用户名和密码
判断用户是否登录成功
select * from user where username=“ ” and password = “ ”;
如果这个sql查询有结果,则之成功,反之,则失败
-- 创建表
CREATE TABLE USER(
id INT PRIMARY KEY AUTO_INCREMENT, -- 主键自增id
username VARCHAR(32), -- 用户名
PASSWORD VARCHAR(32) -- 密码
);
-- 添加数据
INSERT INTO USER VALUES(NULL,'TOM',123);
INSERT INTO USER VALUES(NULL,'Jerry',456);
-- 查询
SELECT * FROM USER;

/*
需求:通过键盘录入用户名和密码
判断用户是否登录成功
*/
public class JDBC_demo03lx {
public static void main(String[] args) throws SQLException {
//1.键盘录入,接受用户和密码
Scanner sc=new Scanner(System.in);
System.out.println("输入用户名:");
String username=sc.nextLine();
//a' or 'a' = 'a万能密码,不论用户名输入什么都可以用万能密码登录
System.out.println("输入密码:");
String password=sc.nextLine();
//2.调用方法,非静态方法new一个对象
boolean flag = new JDBC_demo03lx().login(username, password);
//3.判断结果,输出不同语句
if (flag){
System.out.println("成功");
}else{
System.out.println("失败");
}
}
/*写一个登录方法,有参数用户和密码,返回是否登陆成功*/
public boolean login(String username, String password) throws SQLException {
//判断录入的密码和用户名是否为空
if (username == null || password == null) {
return false;
}
//连接数据库判断是否登陆成功
Connection conn = null;
Statement stmt=null;
ResultSet rs=null;
//1.获取链接
try {
conn = JDBCUtils.getConnection();
//2.定义sql语句
String sql="select * from user where username='"+username+"' and password='"+password+"'";
//3.获取执行sql的对象
stmt = conn.createStatement();
//4.执行查询
rs = stmt.executeQuery(sql);
//5.判断结果集是不是有数据
return rs.next(); //返回为布尔型数据,如果有下一行就返回true
} catch (SQLException e) {
e.printStackTrace();
}finally {
//6.释放资源
JDBCUtils.close(rs,stmt,conn);
}
return false;
}
}


浙公网安备 33010602011771号