package com.pmjava.jdbcdb;import java.io.PrintStream;import java.sql.*;import java.util.Properties;import javax.sql.DataSource;public class DBConnect{ private static DataSource ds; private Connection conn; private Statement stmt; private PreparedStatement prepstmt; private ResultSet rs; private String dbDriver; private String dbUrl; private String dbUser; private String dbPassword; private DBConnect() { conn = null; stmt = null; prepstmt = null; rs = null; dbDriver = ""; dbUrl = ""; dbUser = ""; dbPassword = ""; try { init(); try { Class.forName(dbDriver); } catch (ClassNotFoundException e) { System.out.println("引导数据库驱动错误:" + e.getMessage ()); } conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword); conn.setAutoCommit(true); stmt = conn.createStatement(); } catch (SQLException e) { System.out.println("创建数据库连接错误:" + e.getMessage()); } } private void init() { java.io.InputStream is = getClass().getResourceAsStream ("/database.properties"); Properties dbProps = new Properties(); try { dbProps.load(is); dbDriver = dbProps.getProperty("driver", "com.mysql.jdbc.Driver"); dbUrl = dbProps.getProperty("url", "jdbc:mysql://localhost/db? auToreconnect=true&defaultAutoCommit=false&useUnicode=true&characterEncoding=gbk "); dbUser = dbProps.getProperty("username", "1111111"); dbPassword = dbProps.getProperty("password", "111111"); } catch (Exception e) { System.err.println("不能读取属性文件:请确保database.properties在 CLASSPATH指定的路径中"); return; } } public static DBConnect getInstance() { return new DBConnect(); } public DataSource getDataSource() { return ds; } public Connection getConnection() { return conn; } public void setAutoCommit(boolean bool) throws SQLException { conn.setAutoCommit(bool); } public void prepareStatement(String sql) throws SQLException { clearParameters(); prepstmt = conn.prepareStatement(sql, 1004, 1008); } public ResultSet executePrepQuery() throws SQLException { if (prepstmt != null) return prepstmt.executeQuery(); else return null; } public int executePrepUpdate() throws SQLException { if (prepstmt != null) return prepstmt.executeUpdate(); else return 0; } public void setString(int index, String value) throws SQLException { prepstmt.setString(index, value); } public void setInt(int index, int value) throws SQLException { prepstmt.setInt(index, value); } public void setBoolean(int index, boolean value) throws SQLException { prepstmt.setBoolean(index, value); } public void setDate(int index, Date value) throws SQLException { prepstmt.setDate(index, value); } public void setLong(int index, long value) throws SQLException { prepstmt.setLong(index, value); } public void setFloat(int index, float value) throws SQLException { prepstmt.setFloat(index, value); } public void setBytes(int index, byte value[]) throws SQLException { prepstmt.setBytes(index, value); } public void setInteger(int index, Integer value) throws SQLException { prepstmt.setLong(index, value.longValue()); } public void setShort(int index, short value) throws SQLException { prepstmt.setShort(index, value); } private void clearParameters() { try { prepstmt.clearParameters(); prepstmt.close(); prepstmt = null; } catch (Exception exception) { } } private void clearStmt() { try { stmt.close(); stmt = null; } catch (Exception exception) { } } public int executeUpdate(String sql) throws SQLException { clearStmt(); if (stmt == null) stmt = conn.createStatement(); return stmt.executeUpdate(sql); } public ResultSet executeQuery(String sql) throws SQLException { clearStmt(); if (stmt == null) stmt = conn.createStatement(1004, 1008); return stmt.executeQuery(sql); } public void commit() throws SQLException { conn.commit(); } public void rollback() throws SQLException { conn.rollback(); } public void close() { try { if (stmt != null) { stmt.close(); stmt = null; } if (prepstmt != null) { prepstmt.clearParameters(); prepstmt.close(); prepstmt = null; } if (conn != null) conn.close(); } catch (SQLException ex) { System.out.println("数据库连接关闭错误:" + ex.getMessage()); } } public static void main(String args[]) { DBConnect DBConnect1 = new DBConnect(); }}
每一件事与每一个美丽景色,都有可能成为一生中的难忘。