运行发生错误
package com.hxh.utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* JDBC的工具类
*/
public class JDBCUtils {
private static final String driverClass;
private static final String url;
private static final String username;
private static final String password;
static {
//加载属性文件并解析:
Properties properties = new Properties();
//如何获得属性文件的输入流?
//通常情况下使用类的加载器的方式进行获取
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
try {
properties.load(is);
} catch (Exception e) {
System.out.println("输入流发生错误");
}
driverClass = properties.getProperty("driverClass");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
System.out.println("hahaha");
}
/**
* 注册驱动的方法
* @throws ClassNotFoundException
*
*/
public static void loadDriver() throws ClassNotFoundException {
Class.forName(driverClass);
}
/**
* 获得连接的方法
* @throws Exception
*/
public static Connection getConnection() throws Exception {
loadDriver();
Connection connection = DriverManager.getConnection(url,username,password);
return connection;
}
/**
* 资源释放
*
*/
public static void release(Statement statement,Connection connection) {
if(statement!=null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
statement=null;
}
}
if(connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
connection=null;
}
}
}
public static void release(ResultSet resultSet,Statement statement,Connection connection) {
if(resultSet!=null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
resultSet=null;
}
}
if(statement!=null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
statement=null;
}
}
if(connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
connection=null;
}
}
}
}package com.hxh.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import org.junit.Test;
import com.hxh.utils.JDBCUtils;
public class JBDCDemo3 {
@Test
//保存记录
public void demo1() {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
System.out.println("begin");
try {
//获得连接
connection = JDBCUtils.getConnection();
System.out.println("goon");
//创建执行SQL语句的对象
statement = connection.createStatement();
//编写SQL
String sql = "SELECT * FROM user";
//执行SQL
resultSet = statement.executeQuery(sql);
while(resultSet.next()) {
System.out.println(resultSet.getInt("uid")+" "+resultSet.getString("username")+" "+resultSet.getString("password")
+ " " + resultSet.getString("name"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.release(resultSet, statement, connection);
}
}
}driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/jdbctest?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8 username=root password=djh,.525787961
实在找不出来是哪里错了,请老师看一下吧
5
收起
正在回答
9回答
1、

同学应该是读取jdbc配置文件的时候有问题,建议打印一下异常:

看看具体的异常是什么,我们再“对症下药”;
猜测是同学的配置文件放置的位置不对,应该放在src目录下,同学这里是不是放在了工程的目录下呢?
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
96年的nash
2019-04-23 18:27:14
package com.hxh.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import org.junit.Test;
import com.hxh.utils.JDBCUtils;
public class JBDCDemo3 {
@Test
// 保存记录
public void demo1(){
Connection conn = null;
Statement stmt = null;
try{
// 获得连接:
conn = JDBCUtils.getConnection();
// 创建执行SQL语句的对象
stmt = conn.createStatement();
// 编写SQL:
String sql = "insert into user values (null,'ggg','123','小六')";
// 执行SQL:
int num = stmt.executeUpdate(sql);
if(num > 0){
System.out.println("保存成功!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
// 释放资源:
JDBCUtils.release(stmt, conn);
}
}
}package com.hxh.utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* JDBC的工具类
*/
public class JDBCUtils {
private static final String driverClass;
private static final String url;
private static final String username;
private static final String password;
static {
//加载属性文件并解析:
Properties properties = new Properties();
//如何获得属性文件的输入流?
//通常情况下使用类的加载器的方式进行获取
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
try {
properties.load(is);
} catch (Exception e) {
System.out.println("输入流发生错误");
}
driverClass = properties.getProperty("driverClass");
System.out.println(driverClass);
url = properties.getProperty("url");
System.out.println(url);
username = properties.getProperty("username");
System.out.println(username);
password = properties.getProperty("password");
System.out.println(password);
}
/**
* 注册驱动的方法
* @throws ClassNotFoundException
*
*/
public static void loadDriver() throws ClassNotFoundException {
Class.forName(driverClass);
}
/**
* 获得连接的方法
* @throws Exception
*/
public static Connection getConnection() throws Exception {
loadDriver();
Connection connection = DriverManager.getConnection(url,username,password);
return connection;
}
/**
* 资源释放
*
*/
public static void release(Statement statement,Connection connection) {
if(statement!=null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
statement=null;
}
}
if(connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
connection=null;
}
}
}
public static void release(ResultSet resultSet,Statement statement,Connection connection) {
if(resultSet!=null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
resultSet=null;
}
}
if(statement!=null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
statement=null;
}
}
if(connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
connection=null;
}
}
}
}driverClass=com.mysql.jdbc.Driver url=jdbc:mysql:///jdbctest?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8 username=root password=djh,.525787961

请老师尽快看一下,谢谢啦
相似问题
登录后可查看更多问答,登录/注册
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程



恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星