JDBC工具类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC工具类相关的知识,希望对你有一定的参考价值。
package com.mycompany.util; 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工具类 * @author Administrator * */ public class JDBCUtil { /** * 获取Connection * @return 返回JDBC的Connection对象 * @throws ClassNotFoundException */ public static Connection getConnection() throws Exception{ //加载属性文件 InputStream inputStream = JDBCUtil.class.getClassLoader() .getResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(inputStream); String url = properties.getProperty("jdbc.url"); String user = properties.getProperty("jdbc.user"); String password = properties.getProperty("jdbc.password"); String driverClass = properties.getProperty("jdbc.driverClass"); Class.forName(driverClass); Connection connection = DriverManager.getConnection(url, user, password); return connection; } /** * 释放资讯 * @param resultSet * @param statement * @param connection */ public static void release(ResultSet resultSet,Statement statement,Connection connection){ if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
本文出自 “素颜” 博客,谢绝转载!
以上是关于JDBC工具类的主要内容,如果未能解决你的问题,请参考以下文章