package cn.itcast.util;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.Properties;
/**
* @author KuKaFei.Hai
* @date 2020/5/6 : 17:02
*/
public class JDBCutil {
private static String url;
private static String user;
private static String password;
private static String driver;
/**
* 获取jdbc连接对象
*
* @return 返回一个连接对象
*/
static { //静态代码块
try {
//创建集合类
Properties proConfig = new Properties();
// 怎么获取配置文件路径? 获取src下的路径下的文件->ClassLoader,所以,配置文件建立在src下
ClassLoader confClass = JDBCutil.class.getClassLoader();
//通过类加载器对象来,在src下按名字来加载文件
URL urL = confClass.getResource("jdbcInfo.properties");
assert urL != null;
String confPath = urL.getPath();
//加载配置文件
proConfig.load(new FileReader(confPath));
//获取属性一 赋值
url = proConfig.getProperty("url");
user = proConfig.getProperty("user");
password = proConfig.getProperty("password");
driver= proConfig.getProperty("driver");
//注册驱动
Class.forName(driver);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
/**
* 关闭非查询的连接对象
*
* @param stmt Statement对象
* @param conn Connection对象
*/
public static void closeAll( Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 关闭查询连接资源对象
*
* @param rs 查询结果集对象 ResultSet
* @param stmt stmt Statement对象
* @param conn conn Connection对象
*/
public static void closeAll(ResultSet rs, Statement stmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
closeAll(stmt,conn);
}
}
连接方法:
package cn.itcast.jdbc;
import cn.itcast.util.JDBCutil;
import java.sql.*;
import java.util.Scanner;
/**
* @author KuKaFei.Hai
* @date 2020/5/8 : 15:40
*/
public class JDBCForLogin {
public static void main(String[] args) {
//录入用户名,密码
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String userName = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
boolean bloFlag = new JDBCForLogin().loginService(userName, password);
if (bloFlag) {
System.out.println("登录成功!" + userName + ",欢迎使用系统");
} else {
System.out.println("登录失败!");
}
}
public boolean loginService(String name, String password) {
//判断传进来的是否为空值
if (name == null || password == null) {
return false;
}
//连接数据库,检验name ,password
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
conn = JDBCutil.getConnection();
//sql ? 占位符,防注入
String sql = "select * from user where username = ? and password = ?";
//用 prepareStatement 对象来引入SQL,防注入
pstmt = conn.prepareStatement(sql);
//赋值
pstmt.setString(1,name);
pstmt.setString(2,password);
//执行语句
rs = pstmt.executeQuery();
//处理结果
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCutil.closeAll(rs, pstmt, conn);
}
return false;
}
}
url=jdbc:mysql://172.16.1.9/db4
user=root
password=***************
driver=com.mysql.jdbc.Driver