可配置的数据库连接类

来源:互联网 发布:双色球参选数据2017 编辑:程序博客网 时间:2024/05/17 04:34
//可配置的数据库连接类
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.Driverurl=jdbc:mysql://127.0.0.1/ref?useUnicode=true&characterEncoding=utf-8username=rootpassword=
使用:

<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 blocke.printStackTrace();}finally{DataBase.freeStatement(conn, pstmt);}



0 0
原创粉丝点击