可配置的数据库连接类

//可配置的数据库连接类
package database;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.sql.PreparedStatement;

public class DataBase {
	 	static String driver = null;
	    static String url = null;
	    static String username = null;
	    static String password = null;
	    
	 //连接前的初始化,加载驱动
	    static {
	        setDataBase();
	        try {
	            Class.forName(driver);
	            System.out.println("驱动加载成功");
	        } catch (ClassNotFoundException e) {
	            e.printStackTrace();
	        }
	    }
	    private static void setDataBase() {
	        String filePath = DataBase.class.getResource("").toString();
	        try {
	            filePath = DataBase.class.getResource("").toURI().getPath();
	        } catch (URISyntaxException ex) {
	            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
	        }
	        File file = new File(filePath + "sql.txt");
	        String files = null;
	        int index = 0;
	        int flag = 1;
	        try {
	            FileInputStream filein = new FileInputStream(file);
	            BufferedReader read = new BufferedReader(new InputStreamReader(filein));
	            try {
	                while ((files = read.readLine()) != null) {
	                    index = files.indexOf("=");
	                    switch (flag) {
	                        case 1:
	                        	driver = files.substring(index + 1, files.length());
	                        case 2:
	                            url = files.substring(index + 1, files.length());
	                        case 3:
	                            username = files.substring(index + 1, files.length());
	                        case 4:
	                            password = files.substring(index + 1, files.length());
	                    }
	                    flag++;
	                }
	            } catch (IOException ex) {
	                Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
	            }
	        } catch (FileNotFoundException ex) {
	            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
	        }
	    }
	    //得到数据库连接
	    public static Connection getConnection() {
	        Connection conn = null;
	        try {
	            conn = (Connection) DriverManager.getConnection(url, username, password);
	            //System.out.println("连接创建成功");
	        } catch (SQLException e) {
	            System.out.println("连接创建失败\n\n");
	            e.printStackTrace();
	        }
	        return conn;
	    }

	    public static void free(ResultSet rs, Connection conn, java.sql.PreparedStatement pre) {
	        try {
	            if (rs != null) {
	                rs.close();
	            }
	        } catch (SQLException e) {
	            System.out.println("关闭失败");
	            e.printStackTrace();
	        } finally {
	            try {
	                if (conn != null) {
	                    conn.close();
	                }
	            } catch (SQLException e) {
	                System.out.println("关闭失败");
	                e.printStackTrace();
	            } finally {
	                try {
	                    if (pre != null) {
	                        pre.close();
	                    }
	                } catch (SQLException e) {
	                    System.out.println("关闭失败");
	                }
	            }
	        }
	    }
	    public static void freeStatement(Connection conn, Statement stmt) {

	        try {
	            if (conn != null) {
	                conn.close();
	            }
	        } catch (SQLException e) {
	            System.out.println("关闭失败");
	            e.printStackTrace();
	        } finally {
	            try {
	                if (stmt != null) {
	                	stmt.close();
	                }
	            } catch (SQLException e) {
	                System.out.println("关闭失败");
	            }
	        }
	    }
	  public static void main(String[] args){
		  System.out.println("\ntest\n");
		  DataBase con = new DataBase();
		  con.getConnection();
	  }  
	    
}

配置文件与DataBase.java放在同文件夹下:

sql.txt

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1/ref?useUnicode=true&characterEncoding=utf-8
username=root
password=
使用:

<span style="white-space:pre">		</span>Connection conn = (Connection) DataBase.getConnection();
		PreparedStatement pstmt = null;
		try {
			pstmt =  conn.prepareStatement(sql);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DataBase.freeStatement(conn, pstmt);
		}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值