jdbc-------JDBCUtil类 工具类
生活随笔
收集整理的這篇文章主要介紹了
jdbc-------JDBCUtil类 工具类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
jdbcutil 主要處理的是 連接數據庫, 和關閉各個流
?
1, 數據庫連接的配置信息: mysql.properties (在工程的目錄下)個人配置
url=jdbc:mysql://localhost:3306/test driver=com.mysql.jdbc.Driver username=root password=1232, 獲取連接
讀取配置信息,加載驅動。連接。(這個在后面的例子常用到)
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();}}}
?
轉載于:https://www.cnblogs.com/lijins/p/10122063.html
總結
以上是生活随笔為你收集整理的jdbc-------JDBCUtil类 工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何清洁与 iPhone 12 搭配使用
- 下一篇: 多多视频如何看历史记录