SurfaceNine 2020-08-07 14:33 采纳率: 0%
浏览 183

java.lang.NoClassDefFoundError解决不了,具体代码如下,有没有大佬能帮我看一下,求求了

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
  • 写回答

4条回答 默认 最新

  • jingluan666 2020-08-07 14:51
    关注

    你这个跟下面这个及其类似,检查下是否导入了mysql-connector的jar包
    https://blog.csdn.net/u013317172/article/details/82806889

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名