会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 134351个问题
JAVA 全系列/第七阶段:项目管理与SSM框架/Mybatis 15841楼

无法初始化类com.bjsxt.JdbcUtil文件。

image.png

工具类代码:

package com.bjsxt;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

/**
 * jdbc工具类
 */
public class JdbcUtil {
    private static String driver;
    private static String jdbcUrl;
    private static String username;
    private static String userpassword;
    static {
        //读取Properties文件
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc.properties");
        driver = bundle.getString(driver);
        jdbcUrl = bundle.getString(jdbcUrl);
        username = bundle.getString(username);
        userpassword = bundle.getString(userpassword);
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //获取Connection对象
    public static Connection getConnection(){
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(jdbcUrl,username,userpassword);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    //关闭Statement
    public static void closeStatement(Statement state){
        try {
             if(state != null){
                state.close();
                }
             } catch (SQLException throwables) {
                throwables.printStackTrace();
             }

    }
    //关闭Connection
    public static void closeConnection(Connection conn){
        try {
            if(conn != null){
                conn.close();
            }
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }
    }
    //关闭资源
    public static void closeResource(Statement state,Connection conn){
        closeStatement(state);
        closeConnection(conn);
    }
}

propenties:

driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8
username=root
userpassword=root

测试:

package com.bjsxt;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class JdbcTest {
    //向Departments表中添加一条数据
    public void insertDepartments(String department_name,int location_id){
        Connection conn = null;
        Statement state = null;
        //驱动注册
        try {
            conn = JdbcUtil.getConnection();
            String sql="insert into departments values(default,'"+department_name+"',"+location_id+")";
            state = conn.createStatement();
            int flag = state.executeUpdate(sql);
            System.out.println(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            JdbcUtil.closeResource(state,conn);
        }
    }
    //更新departments表中的department_id为6的数据,将部门名称修改为教学部,location_id修改为6
    public void updateDepartments(String department_name,int location_id,int department_id){
        Connection conn = null;
        Statement state = null;
        try {
            conn = JdbcUtil.getConnection();
            state = conn.createStatement();
            String sql = "update departments d set d.department_name='"+department_name+"',d.location_id="+location_id+" where d.department_id="+department_id;
            int flag = state.executeUpdate(sql);
            System.out.println(flag);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JdbcUtil.closeResource(state,conn);
        }
    }
    public static void main(String[] args) {
        JdbcTest test = new JdbcTest();
        test.insertDepartments("教学部",9);
        //test.updateDepartments("研发部",8,6);
    }
}

问题应该在properties文件读取不到,绝对路径试过了,一模一样的报错,不读取properties文件没有问题。请老师讲解

JAVA 全系列/第四阶段:数据库与AI协同技术实战/JDBC技术(旧) 15842楼
Python全系列/第七阶段:网页编程基础/Ajax 15843楼
Python全系列/第一阶段:AI驱动的Python编程/序列 15846楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO流技术 15847楼
Python全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask高级 15848楼
JAVA 全系列/第十三阶段:高性能数据处理、NoSQL、分库分表/Redis 15849楼

image.png

travel.zip

这是为什么呢?

JAVA 全系列/第十三阶段:百战旅游网项目/百战旅游网 15850楼
JAVA 全系列/第六阶段:JavaWeb开发/Servlet技术详解(旧) 15851楼

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./jquery-3.6.1.min.js"></script>
</head>

<body>
    <script>
        function ajax(url) {
            return new Promise((resolve, reject) => {
                $.getJSON(url, function (data) {
                    resolve(data);
                }, function (err) {
                    reject(err);
                });
            });
        }
        async function getinfo1() {
            var ids = await ajax("http://iwenwiki.com/api/generator/list.php");
            var names = await ajax("http://iwenwiki.com/api/generator/id.php?id=" + ids[0]);
            var infos = await ajax("http://iwenwiki.com/api/generator/name.php?name=" + names.name);
            console.log(infos);
        }
        getinfo1();
        async function getinfo2() {
            var ids1 = await ajax("http://iwenwiki.com/api/generator/list.php");
            var names1 = await ajax("http://iwenwiki.com/api/generator/id.php?id=" + ids1[0]);
            var infos1 = await ajax("http://iwenwiki.com/api/generator/name.php?name=" + names1.name);
            return infos1;
        }
        console.log(getinfo2());
       
       
    </script>
</body>

</html>

image.png

getinfo1函数正常输出,getinfo2函数输错错误,,为什么??

WEB前端全系列/第七阶段:ECMAScript6新特性模块/ES6 第一部分 15852楼
JAVA 全系列/第十七阶段:前后端分离技术VUE/ES6新特性 15854楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637