jdbc-------JDBCUtil类 工具类
生活随笔
收集整理的這篇文章主要介紹了
jdbc-------JDBCUtil类 工具类
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
jdbcutil 主要處理的是 連接數(shù)據(jù)庫, 和關(guān)閉各個(gè)流
?
1, 數(shù)據(jù)庫連接的配置信息: mysql.properties (在工程的目錄下)個(gè)人配置
url=jdbc:mysql://localhost:3306/test driver=com.mysql.jdbc.Driver username=root password=1232, 獲取連接
讀取配置信息,加載驅(qū)動(dòng)。連接。(這個(gè)在后面的例子常用到)
package com.ljs.util;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class JDBCUtil {private static String url;private static String user;private static String password;private static String driver;static{try {Properties properties = new Properties();FileInputStream fis = new FileInputStream(new File("mysql.properties"));properties.load(fis);url = properties.getProperty("url");user = properties.getProperty("username");password = properties.getProperty("password");driver = properties.getProperty("driver");Class.forName(driver);} catch (Exception e) {e.getMessage();}}public static Connection getConn() throws Exception{Connection connection = DriverManager.getConnection(url, user, password);return connection;}public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection){try {if (resultSet != null) {resultSet.close();}if(preparedStatement != null ){preparedStatement.close();}if(connection != null ){connection.close();}} catch (SQLException e) {throw new RuntimeException();}}}
?
轉(zhuǎn)載于:https://www.cnblogs.com/lijins/p/10122063.html
總結(jié)
以上是生活随笔為你收集整理的jdbc-------JDBCUtil类 工具类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何清洁与 iPhone 12 搭配使用
- 下一篇: 多多视频如何看历史记录