编写jdbc工具类JDBCUtils

通过配置文件(jdbc.properties)来加获取加载驱动、url、user、password等信息,该配置文件在src文件目录下

jdbc.properties

url=jdbc:mysql://localhost:3306/db_study
user=root
password=root
driver=com.mysql.jdbc.Driver

JDBCUtils

package Util;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtils {
	static String url;
	static String user;
	static String password;
	static String driver;
	
	
	static{
		
		try {
			Properties pro = new Properties();
			ClassLoader classLoader = JDBCUtils.class.getClassLoader();
			URL res = classLoader.getResource("jdbc.properties");
			String path = res.getPath();
			path = java.net.URLDecoder.decode(path,"utf-8");
			System.out.println(path);
			pro.load(new FileReader(path));
			
			
			url = pro.getProperty("url");
			user = pro.getProperty("user");
			password = pro.getProperty("password");
			driver = pro.getProperty("driver");
			
			//加载驱动
			Class.forName(driver);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 创建连接
	 * @return Connection
	 */
	public static Connection getConnection(){
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(url,user,password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * 资源释放
	 * @param conn
	 * @param stmt
	 * @param rs
	 */
	public static void close(Connection conn,Statement stmt,ResultSet rs){
		if(conn != null){
			try {
				conn.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		if(stmt != null){
			try {
				stmt.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		if(rs != null){
			try {
				rs.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}
	/**
	 * 
	 * @param conn
	 * @param stmt
	 */
	public static void close(Connection conn,Statement stmt){
		if(conn != null){
			try {
				conn.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
		if(stmt != null){
			try {
				stmt.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		
	}
}

使用JDBCUtils工具类查看test02表中的数据

package Demo02_ResultSet;

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

import Util.JDBCUtils;

public class Demo01_ResultSet {

	public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try{
			//Class.forName("com.mysql.jdbc.Driver");
			//String sql = "create table test02(name varchar(255), age int)";
			String sql = "select * from test02";
			//conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_study","root","qqq7612838");
			conn = JDBCUtils.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			
			//遍历rs集合
			while(rs.next()){
				int id = rs.getInt("id");//1
				String name = rs.getString("name");//2
				int age = rs.getInt("age");//3
				System.out.println(id + "\t" + name + "\t" + age);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			JDBCUtils.close(conn, stmt,rs);
			/*
			if(conn != null){
				try {
					conn.close();
				} catch (Exception e) {
					// TODO: handle exception
				};
			}
			
			if(stmt != null){
				try {
					stmt.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
			
			if(rs != null){
				try {
					rs.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
			*/
			
		}
	}
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兴奋の大公猴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值