10

写了一个propertie配置文件,测试的时候曝出 Communications link failure错误


```本人正在学习中,然后跟着教程写了一个连接数据库的properties配置文件,测试的时候发现报错Communications link failure。
搜了一些问答
例如修改wait_timeout的我也做了
![图片说明](https://img-ask.csdn.net/upload/201906/21/1561087418_663225.png)
再试还是这样,又担心是不是版本不匹配,检查了一遍也没看出问题,下面把我的配置和代码贴出来,希望有哪位大佬不吝举手之劳帮我看看问题
这是我的jar包

mysql-connector-java-5.1.37-bin.jar
commons-dbutils-1.6.jar

报错:
```java.io.BufferedInputStream@15db9742
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

测试代码如下:
```public class PropertiesDemo {
public static void main(String[] args) throws Exception {
//使用类的加载器
InputStream in = PropertiesDemo.class.getClassLoader().getResourceAsStream("database.properties");
System.out.println(in);
Properties pro = new Properties();
pro.load(in);
//System.out.println(pro);

       String driverClass = pro.getProperty("driverClass");
       String url = pro.getProperty("url");
       String username = pro.getProperty("username");
       String password = pro.getProperty("password");
       Class.forName(driverClass);
       Connection con = DriverManager.getConnection(url, username, password);
       System.out.println(con);
后面我不信邪又另外测试了下:

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;

public class Test3 {
public static void main(String[] args) {
insert();
}
public static void insert(){
try {
//获取一个用来执行SQL语句的对象 QueryRunner
QueryRunner qr = new QueryRunner();

        String sql = "INSERT INTO zhangwu(name,money,parent) VALUES(?,?,?)";
        Object[] params = {"股票收入", 5500, "收入"};
        Connection conn = JDBCUtils.getConnection();
        int line = qr.update(conn,sql,params);// 用来完成表数据的增加、删除、更新操作
        //结果集处理
        System.out.println("line = " + line);

    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

}

报错:

Exception in thread "main" java.lang.ExceptionInInitializerError
at com.itheima_dbutils.Test3.insert(Test3.java:19)
at com.itheima_dbutils.Test3.main(Test3.java:10)
Caused by: java.lang.RuntimeException: java.io.FileNotFoundException: database.properties (系统找不到指定的文件。)
at com.itheima_dbutils.JDBCUtils.(JDBCUtils.java:28)
... 2 more
Caused by: java.io.FileNotFoundException: database.properties (系统找不到指定的文件。)

然后把配置文件贴一下吧

driverClass = com.mysql.jdbc.Driver
url = jdbc:mysql://locahost:3306/mybase
username = root
password = 123

最后是我的引用工具类

package com.itheima_dbutils;

import java.io.FileReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class JDBCUtils {
private static String driverClass;
private static String url;
private static String username;
private static String password;
// 静态代码块
static {
try {
// 1 使用Properties处理流
// 使用load()方法加载指定的流
Properties props = new Properties();
Reader is = new FileReader("database.properties");
props.load(is);
// 2 使用getProperty(key),通过key获得需要的值,
driverClass = props.getProperty("driverClass");
url = props.getProperty("url");
username = props.getProperty("username");
password = props.getProperty("password");
} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
 * 获得连接
 */
public static Connection getConnection() {
    try {
        // 1 注册驱动
        Class.forName(driverClass);
        // 2 获得连接
        Connection conn = DriverManager.getConnection(url, username, password);
        return conn;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

}

查看全部
qq_42002361
qq_42002361
2019/06/21 12:11
  • java
  • eclipse
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

3个回复