bug模样:Exception in thread “main” java.lang.NoClassDefFoundError: Could not initialize class com.ibentu.jdbc.JdbcBase
能输入用户名,也能输入密码,就是不能够在数据库校验,为什么呢?在查了众多建议之后,有说因为jar包问题,官方文档那生涩的解释更是不能理解。
仔细检查了一遍代码,原来是抽取Base类的时候,一不小心把properties的路径弄扯了
将此处改为下面的样子即可,因为此路径默认在src下面寻找
之后运行,OK了
附上源码:此源码有SQL注入风险
JdbcBase.java
/**
* JDBC工具类
*/
public class JdbcBase {
private static String url;
private static String user;
private static String password;
private static String driver;
/**
* 文件的读取只需要读取一次,即可拿到这些值,使用静态代码块
*/
static {
//读取资源文件、获取值
try {
//1.创建Properties集合类
Properties properties = new Properties();
//2.获取src路径下文件的方式--->ClassLoader 类加载器
ClassLoader classLoader = JdbcBase.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
String path = res.getPath();
System.out.println(path);
//3.加载文件
properties.load(new FileReader(path));
//4.获取数据,赋值
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
driver = properties.getProperty("driver");
//5.注册驱动
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取连接
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
/**
* 释放资源
* @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();
}
}
}
}
UserRegister.java
public class UserRegister {
public static void main(String[] args) {
//1.键盘录入,接收用户名和密码
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名");
String username = scanner.nextLine();
System.out.println("请输入密码");
String password = scanner.nextLine();
//2.调用方法
UserRegister userRegister = new UserRegister();
boolean flag = userRegister.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;
Statement statement = null;
ResultSet resultSet =null;
try {
//1.获取连接
connection = JdbcBase.getConnection();
//2.定义sql
String sql = "select * from user where username = '" + username+"' and password = '" + password + "' ";
//3.获取执行sql的对象
statement = connection.createStatement();
//4.执行查询
resultSet = statement.executeQuery(sql);
//5.判断
/*if (resultSet.next()){//如果有下一行,则返回true
return true;
}else {
return false;
}*/
return resultSet.next();
} catch (SQLException e) {
e.printStackTrace();
}finally {
System.out.println("finally未执行");
JdbcBase.close(resultSet, statement, connection);
System.out.println("finally已执行");
}
return false;
}
}
数据库user表
----------------------------Happy End----------------------------------