jdbc-------JDBCUtil類 工具類


jdbcutil 主要處理的是 連接數據庫, 和關閉各個流

 

1, 數據庫連接的配置信息: mysql.properties (在工程的目錄下)個人配置

url=jdbc:mysql://localhost:3306/test
driver=com.mysql.jdbc.Driver
username=root
password=123

2, 獲取連接

讀取配置信息,加載驅動。連接。(這個在后面的例子常用到)

 
         

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(); } } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM