把注册驱动放到JDBC工具类

改动的地方:
JDBCTest:

JDBCUtil:

JDBCTest:

package com.test.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.Driver;
import com.test.util.JDBCUtil;

public class JDBCTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			//1. 注册驱动 
			connection = JDBCUtil.getConn();
	/*	
			//Driver 这个类里面有静态代码块,一上来就执行了,所以等同于我们注册了两次驱动。 其实没这个必要的。
		   	//静态代码块 ---> 类加载了,就执行。 java.sql.DriverManager.registerDriver(new Driver());
			//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
		
				Class.forName("com.mysql.jdbc.Driver");
			
			//2. 建立连接 参数一: 协议 + 访问的数据库 , 参数二: 用户名 , 参数三: 密码。
			 connection = DriverManager.getConnection("jdbc:mysql://localhost/jdbc", "root", "root");*/
			//3. 创建statement , 跟数据库打交道,一定需要这个对象
			 statement = connection.createStatement();
			//4. 执行查询 , 得到结果集
			String sql = "select * from stu";
            resultSet = statement.executeQuery(sql);
			//5. 遍历查询每一条记录
			while(resultSet.next()){
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				
				System.out.println("id="+id+",name="+name+",age="+age);
			}
//			resultSet.close();
//			Statement.close();
//			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.close(connection, resultSet, statement);
		}

	}

}


JDBCUtil:
 

package com.test.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUtil {
	
	static String url = "jdbc:mysql://localhost/jdbc";
	static String name = "root";
	static String password = "root";
	
	/**
	 * 注册驱动 建立参数
	 * <p>Title: close</p>
	 * <p>Description: </p>
	 * @param connection
	 * @param resultSet
	 * @param statement
	 */
	
	public static Connection getConn(){
			
		Connection connection = null;
		//2. 建立连接 参数一: 协议 + 访问的数据库 , 参数二: 用户名 , 参数三: 密码。
		 try {
			 Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection(url, name, password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return connection;
		
	}
	
	/**
	 * 释放资源
	 * <p>Title: close</p>
	 * <p>Description: </p>
	 * @param connection
	 * @param resultSet
	 * @param statement
	 */
	public static void close(Connection connection,ResultSet resultSet,Statement statement){
		closeRS(resultSet);
		closeSt(statement);
		closeConn(connection);
		
	}
	
	private static void closeRS(ResultSet resultSet){
		try {
			if(resultSet !=null){
				resultSet.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			resultSet = null;
		}
	}
	
	private static void closeSt(Statement statement){
		try {
			if(statement !=null){
				statement.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			statement = null;
		}
	}
	
	private static void closeConn(Connection connection){
		try {
			if(connection !=null){
				connection.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			connection = null;
		}
	}
	


}

数据库:

运行结果:
 

猜你喜欢

转载自blog.csdn.net/mqingo/article/details/84503872