程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java鏈接數據庫的一個CLASS

java鏈接數據庫的一個CLASS

編輯:關於JAVA

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved