运行发生错误

来源:4-2 JDBC的工具类的抽取二

96年的nash

2019-04-23 14:19:28

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

实在找不出来是哪里错了,请老师看一下吧

写回答

9回答

好帮手慕阿莹

2019-04-24

1、

http://img.mukewang.com/climg/5cbfcbea00012ba608660242.jpg

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

http://img.mukewang.com/climg/5cbfcc380001d32b12140362.jpg

看看具体的异常是什么,我们再“对症下药”;

猜测是同学的配置文件放置的位置不对,应该放在src目录下,同学这里是不是放在了工程的目录下呢?

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

0
h6年的nash
h 放错地方了,感谢老师回答
h019-04-24
共1条回复

好帮手慕阿满

2019-04-23

同学你好,试验同学的代码,可以正常运行,也输出了url等信息,如:

http://img.mukewang.com/climg/5cbeee5a0001e7bc06110112.jpg

问一下同学是mysql8的版本吗?如果是,建议同学将driverClass改为com.mysql.cj.jdbc.Driver。另外建议同学检查一下用户名及密码是否正确。

祝:学习愉快~

1
h6年的nash
h 请老师尽快解答一下,辛苦了
h019-04-24
共2条回复

96年的nash

提问者

2019-04-24

http://img.mukewang.com/climg/5cbfc33a0001e31004500319.jpg

0

96年的nash

提问者

2019-04-24

请老师尽快处理一下,等了好久了,谢谢老师了

0

96年的nash

提问者

2019-04-23

请老师尽快解答一下,这个问题解决不了,下面的课都没法上了

0

96年的nash

提问者

2019-04-23

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

http://img.mukewang.com/climg/5cbee86e0001c19e12090675.jpg

请老师尽快看一下,谢谢啦

0

好帮手慕阿满

2019-04-23

同学你好,看到同学在代码中有输出“hahaha”,“begin”等内容,这些完全没有输出吗?同学输出null的语句是在哪里添加的呢?建议同学将修改后的代码贴一下。

祝:学习愉快~

0

96年的nash

提问者

2019-04-23

http://img.mukewang.com/climg/5cbed1e70001c19e12090675.jpg

这里错了,username,password都接收不到

0

好帮手慕阿莹

2019-04-23

1、请同学贴一下你的报错信息哦,不然老师不能准确的定位同学的问题,请同学贴一下你的报错信息的截图。如果直接复制粘贴,请不要贴到回复里,会失去格式,可以在“我要回答”中贴一下

祝学习愉快。

0

0 学习 · 8016 问题

查看课程