//依赖
jdbc.properties 内容:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/shiro?useUnicode=true&characterEncoding=utf-8&useSSL=false
username=root
password=123456
package com.cxb.shiro;
import java.io.IOException;
import java.util.Properties;
/**
* 获取资源文件的util
* @author 81046
*
*/
public class PropertiesUtil {
static Properties properties = new Properties();
public PropertiesUtil() {
}
public static boolean loadFile(String fileName){
try {
properties.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName));
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static String getPropertyValue(String key){
return properties.getProperty(key);
}
}
}
/**
* 通过用户名到数据库中获取凭证密码
* @param userName
* @return
*/
private static String getPasswordByUserName(String userName) {
//SQL语句
String sql = "select password from users where username = " +"'" + userName+"'";
Connection conn = JdbcUtils.getConn();
Statement stmt=null;
ResultSet ret = null;
String password=null;
try {
stmt = conn.createStatement();
//执行语句,得到结果集
ret = stmt.executeQuery(sql);
while (ret.next()) {
//这里只查询的密码
password = ret.getString(1);
}
ret.close();
conn.close();//关闭连接
} catch (SQLException e1) {
e1.printStackTrace();
}
return password;
}