package com.sise.jdbc;
import com.sise.util.JdbcUtil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
/**
* 利用jdbc工具类模拟登录功能
*/
public class JdbcTest5 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("输入登录账号:");
String username = s.nextLine();
System.out.println("输入登录密码:");
String password = s.nextLine();
Login(username,password);
}
public static void Login(String u,String p){
if (u==null || p==null){
System.out.println("登录失败");
}
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
try {
connection = JdbcUtil.getConnection();
//sql是用String修饰是字符串类型,变量u和p需要用拼接的手法,才能传递值
String sql="select * from student where name='"+u+"'and password='"+p+"'";
System.out.println(sql);
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
if (resultSet.next()){//如果搜索成功,那么它的next有值返回true,如果返回值是Boolean类型的就直接返回resultSet.next()就可以了,因为它返回boolean类型的值
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
JdbcUtil.close(connection,statement,resultSet);
}
}
}
package com.sise.util;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
/**
* 工具类的作用:简化繁琐重复的代码
* 抽取:连接数据库和释放资源(重复性高)
*
*/
public class JdbcUtil {
/**
* 注意因为静态代码块、静态方法只能访问静态成员变量
*/
private static String url = null;
private static String user = null;
private static String password = null;
private static String driver=null;
/**
* 如果把读取文件代码写到getConnection()方法中,那么每次调用
* getConnection()时都会读取文件,则影响效率,所以把读取文件
* 代码写到外面,只需要读取一次即可,提高效率
*
* 怎样实现呢?可以使用静态代码块,因为它会随着类的加载而加载,所以只会
* 执行一次
* @return
* @throws Exception
*/
static{
try {
/*//url表示统一资源定位符,可以定位一个文件的绝对路径
URL resource = JdbcUtil.class.getClassLoader().getResource("properties.properties");
//获取路径
String path = resource.getPath();*/
InputStream resourceAsStream = JdbcUtil.class.getClassLoader().getResourceAsStream("properties.properties");
Properties p=new Properties();
p.load(resourceAsStream);
url = p.getProperty("url");
user = p.getProperty("user");
password = p.getProperty("password");
//注册驱动
Class.forName(p.getProperty("Driver"));
} catch (IOException e) {
e.printStackTrace();
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception {
return DriverManager.getConnection(url, user, password);
}
/**
* DML需要释放两个资源:Connection c, Statement s
*
* @param c
* @param s
*/
public static void close(Connection c, Statement s) {
if (s != null) {
try {
s.close();
/*c.close();不要这样写,因为假如s.close()报错则直接catch,就不会执行c.close()则导致
资源的浪费
*/
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (c != null) {
try {
c.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
/**
* DQL需要释放三个资源:Connection c, Statement s, ResultSet r
*
* @param c
* @param s
* @param r
*/
public static void close(Connection c, Statement s, ResultSet r) {
if (r != null) {
try {
r.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (s != null) {
try {
s.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (c != null) {
try {
c.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}