package com.ZHY.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
static String driver;
static String url;
static String username;
static String password;
static {
try {
Properties pro = new Properties();
InputStream resource = JDBCUtils.class.getClassLoader().getResourceAsStream("Jdbc.properties");
pro.load(resource);
driver = pro.getProperty("mysql.driver");
url = pro.getProperty("mysql.url");
username = pro.getProperty("mysql.username");
password = pro.getProperty("mysql.password");
} catch (IOException e) {
e.printStackTrace();
}
}
//获取数据库连接
public static Connection getConnection(){
Connection conn = null;
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return conn;
}
//关闭资源
public static void close(ResultSet rs, Statement stat, Connection conn){
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (stat != null) {
try {
stat.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void close(Statement stat,Connection conn){
if (stat != null) {
try {
stat.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
package com.ZHY.study;
import com.ZHY.util.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class Demo2 {
/**
* 使用jdbc工具类
* 封装对象:account账户对象
*
* @param
*/
public List<Account> findAll() {
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
List<Account> list = new ArrayList<>();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = JDBCUtils.getConnection();
String sql = "select * from account";
stat = conn.createStatement();
rs = stat.executeQuery(sql);
while (rs.next()){
// int id = rs.getInt(1);
// String name = rs.getString(2);
// String pwd = rs.getString(3);
Account account = new Account();
account.setId(rs.getInt(1));
account.setUsername(rs.getString(2));
account.setPassword(rs.getString(3));
list.add(account);
}
} catch (SQLException | ClassNotFoundException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtils.close(rs,stat,conn);
}
return list;
}
public static void main(String[] args) throws ClassNotFoundException {
List<Account> list = new Demo2().findAll();
for (Account account : list) {
System.out.println(account);
}
}
}
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/zhytest?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
mysql.username=root
mysql.password=zhy2097.
然后是报错信息:
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.ZHY.util.JDBCUtils
at com.ZHY.study.Demo2.findAll(Demo2.java:43)
at com.ZHY.study.Demo2.main(Demo2.java:49)
Process finished with exit code 1