ET之JDBC 数据库驱动、连接 配置文件操作

将注册数据库驱动的信息和连接数据库的操作(要连接的库、用户、密码)等信息写到配置文件,比如config.properties中,key---value形式可以随时修改配置信息,减少代码的重复操作,以及做到代码的模块化 

获取配置文件的内容 (操作如下)

1、   新建配置文件对象 (文件放在当前类中就可以)

2、   创建对象

Properties prop = new Properties();

3、   加载对象  load()方法---FIS流---File

String path = “config.properties”;

prop.load(newFileInputStream(new File(path));  

结构图:

配置文件:

key---value

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///jt_db
user=root
password=root

封装方法:

package com.tedu.jdbc.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;


/**
 * JDBC 工具类,用于就简化JDBC操作
 * @author 000
 *
 */
public class JDBCUtils {
	/*
	 * 构造方法私有化目的是为防止别人创建盖类的实例
	 * 因为工具类的方法都是亭台的 通过类名直接点 引用
	 * 方法就是可以直接调用
	 */
	private JDBCUtils() {}
	
    /**
     * 用于获取一个数据库连接对象	
     * @return
     * @throws Exception
     */
	public static Connection getConn() throws Exception {
		Properties prop = new Properties();
		String path = "config.properties";
		prop.load(new FileInputStream(new File(path)));
		String driverClass = prop.getProperty("driverClass");
		String jdbcUrl = prop.getProperty("jdbcUrl");
		String user = prop.getProperty("user");
		String password = prop.getProperty("password");
		Class.forName(driverClass);
		Connection conn = DriverManager.getConnection(jdbcUrl, user, password);
		return conn;
	}
	
	/**
	 * 动态的JDBC查询
	 */
     public static void JDBCR(String sql){
    	 try {
			Connection conn = JDBCUtils.getConn();
			Statement stat =conn.createStatement();
			ResultSet rs = stat.executeQuery(sql);
			while(rs.next()){
				int id = rs.getInt("id");
				String username = rs.getString("username");
				String password = rs.getString("password");
				System.out.println(id+"-"+username+"-"+password);
			}
			System.out.println("查询完毕!");
		} catch (Exception e) {
			e.printStackTrace();
		}
     }
	
     /**
      *动态的JDBC增删改
     * @throws Exception 
      */
     public static void JDBCUDI(String sql) {
    	 try {
    		 Connection conn = JDBCUtils.getConn();
    		 Statement stat =conn.createStatement();
    		 int rs =stat.executeUpdate(sql);
    		 System.out.println("影响了"+rs+"行");
    		 System.out.println("修改完毕!!");
			
		} catch (Exception e) {
         e.printStackTrace();
		}
    	 
     }
     
	/**
	 * 释放JDBC程序中的资源
	 * @param conn  连接对象
	 * @param stat  传输器对象
	 * @param rs     结果集对象
	 */
	public static void close(Connection conn, Statement stat, ResultSet rs) {
		if (rs != null) {     //可能为null   会出现空指针异常
			try {
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				rs = null;
			}
		}

		if (stat != null) {
			try {
				stat.close();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				stat = null;
			}
		}

		if (conn != null) {
			try {

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				conn = null;
			}
		}
	}
}
Properties prop = new Properties();
		String path = "config.properties";
		prop.load(new FileInputStream(new File(path)));
		String driverClass = prop.getProperty("driverClass");
		String jdbcUrl = prop.getProperty("jdbcUrl");
		String user = prop.getProperty("user");
		String password = prop.getProperty("password");
		Class.forName(driverClass);
		Connection conn = DriverManager.getConnection(jdbcUrl, user, password);
		return conn;
	}
	
	/**
	 * 动态的JDBC查询
	 */
     public static void JDBCR(String sql){
    	 try {
			Connection conn = JDBCUtils.getConn();
			Statement stat =conn.createStatement();
			ResultSet rs = stat.executeQuery(sql);
			while(rs.next()){
				int id = rs.getInt("id");
				String username = rs.getString("username");
				String password = rs.getString("password");
				System.out.println(id+"-"+username+"-"+password);
			}
			System.out.println("查询完毕!");
		} catch (Exception e) {
			e.printStackTrace();
		}
     }
	
     /**
      *动态的JDBC增删改
     * @throws Exception 
      */
     public static void JDBCUDI(String sql) {
    	 try {
    		 Connection conn = JDBCUtils.getConn();
    		 Statement stat =conn.createStatement();
    		 int rs =stat.executeUpdate(sql);
    		 System.out.println("影响了"+rs+"行");
    		 System.out.println("修改完毕!!");
			
		} catch (Exception e) {
         e.printStackTrace();
		}
    	 
     }
     
	/**
	 * 释放JDBC程序中的资源
	 * @param conn  连接对象
	 * @param stat  传输器对象
	 * @param rs     结果集对象
	 */
	public static void close(Connection conn, Statement stat, ResultSet rs) {
		if (rs != null) {     //可能为null   会出现空指针异常
			try {
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				rs = null;
			}
		}

		if (stat != null) {
			try {
				stat.close();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				stat = null;
			}
		}

		if (conn != null) {
			try {

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				conn = null;
			}
		}
	}
}
发布了21 篇原创文章 · 获赞 3 · 访问量 6562

猜你喜欢

转载自blog.csdn.net/MADMY/article/details/80673576