連接工廠,實現了DataSource接口
實現連接SQLServer的連接工廠,這里因為我們的項目使用SQLServer2000所以只實現了SqlServerConnectionFactory。package skydev.modules.data; import java.sql.*; import javax.sql.DataSource; import java.io.PrintWriter; public class ConnectionFactory implements DataSource { private String userName; private String password; private String driverName; private String url; private java.sql.Connection connection; /** * 根據設置的連接參數創建一個新的連接實例 * @return */ private Connection getNewConnection() { try { this.connection.close(); //試圖關閉連接 } finally { this.connection = null; //釋放連接 try { Class.forName(this.driverName); //加載驅動程序 //DriverManager.registerDriver(driver); try { this.connection = DriverManager.getConnection(this.url, this.userName, this.password); } catch (SQLException e) { throw e; } } finally { return this.connection; //返回新建立的連接 } } } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDriverName() { return driverName; } public void setDriverName(String driverName) { this.driverName = driverName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public java.sql.Connection getConnection() { if (connection != null) { try { if (connection.isClosed()) { connection = null; getNewConnection(); } } catch (SQLException ex) { } } if (connection == null) { //沒有設置連接則創建一個連接 getNewConnection(); } return connection; } public Connection getConnection(String userName, String password) throws SQLException { this.setUserName(userName); this.setPassword(password); return getConnection(); } public PrintWriter getLogWriter() { return null; } public void setLogWriter(PrintWriter printWriter) { } public void setLoginTimeout(int int0) { } public int getLoginTimeout() { return 0; } } |
package skydev.modules.data; public final class SqlServerConnectionFactory extends ConnectionFactory { private final String dbDriver ="com.microsoft.jdbc.sqlserver.SQLServerDriver"; private String host;//主機 private int port;//端口 private String databaseName;//Sql數據庫名稱 public SqlServerConnectionFactory() { super.setDriverName(dbDriver); } /** * * @param host 數據庫所在的主機名:如"localhost" * @param port SQL服務器運行的端口號,如果使用缺省值 1433,傳入一個負數即可 * @param databaseName 數據庫名稱 * @param userName 用戶名 * @param password 口令 */ public SqlServerConnectionFactory(String host, int port, String databaseName, String userName, String password) { this.setHost(host); this.setPort(port); this.setDatabaseName(databaseName); this.setUserName(userName); this.setPassword(password); init(); } private void init() { super.setDriverName(dbDriver); super.setUrl("jdbc:microsoft:sqlserver://" + host.trim() + ":" + new Integer(port).toString() + ";DatabaseName=" + databaseName.trim()); //super.setUrl("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=demo"); } public void setHost(String host) { //處理主機名稱 if ( (host == null) || (host.equals("")) || (host.equals(".")) || (host.equals("local"))) { host = "localhost"; } int index = host.indexOf("http://", 0); if (index == 0) { host = host.substring(2); //去掉前面的"http://" } index = host.indexOf("http://", 0); if (index >= 0) { try { throw new Exception("SQL Server主機名參數錯誤!"); } catch (Exception ex) { } } this.host = host; } public void setPort(int port) { /** * 缺省端口1433 */ if (port < 0) { port = 1433; } this.port = port; } public void setDatabaseName(String databaseName) { this.databaseName = databaseName; } } 使用"sun.jdbc.odbc.JdbcOdbcDriver"連接數據庫的連接工廠 package skydev.modules.data; public class JdbcOdbcConnectionFactory extends ConnectionFactory { private final static String driveName = "sun.jdbc.odbc.JdbcOdbcDriver"; private String odbcName; public JdbcOdbcConnectionFactory() { super.setDriverName(driveName); } /** *使用指定的Odbc數據源連接數據庫服務器 * @param odbcName */ public JdbcOdbcConnectionFactory(String odbcName) { super.setDriverName(driveName); setOdbcName(odbcName); } public void setOdbcName(String odbcName) { this.odbcName = odbcName; this.setUrl("jdbc:odbc:" + odbcName); } } |
package skydev.modules.data; import java.sql.*; import java.sql.PreparedStatement; import javax.sql.DataSource; public abstract class DatabaseObject { protected Connection connection = null; protected ResultSet resultSet = null; protected ResultSetMetaData resultSetMetaData = null; private ConnectionFactory connectionFactory = null; private java.sql.Statement statement=null; private javax.sql.DataSource dataSource;//=new Statement(); public DatabaseObject(){ dataSource=null; connection=null; } public DatabaseObject(ConnectionFactory connectionFactory) { this.setConnectionFactory(connectionFactory); this.dataSource=connectionFactory;//ConnectionFactory實現了DataSource接口 } /** * 執行查詢 * @param sql 要執行的Sql語句 * @return 返回查詢的結果集 ,查詢失敗返回null */ public ResultSet getResultSet(String sql) { try { this.resultSet = statement.executeQuery(sql); //保留內部指針 } catch (SQLException e) { e.printStackTrace(); this.resultSet = null; } finally { return this.resultSet; } } /** * 獲取外部指定ResltSet的ResultSetMetaData數據 * @param resultSet 要獲取的ResultSet * @return 失敗返回null */ public ResultSetMetaData getResultSetMetaData(ResultSet resultSet) { ResultSetMetaData resultSetMetaData = null; try { resultSetMetaData = resultSet.getMetaData(); } catch (SQLException e) { e.printStackTrace(); resultSetMetaData = null; } finally { return resultSetMetaData; } } /** * 獲取最近一次設置或者返回的ResultSet的ResultMetaData數據, * 比方說調用了:getResultSet(sql)方法,然后調用getResultSetMetaData方法 * 可以獲得相應的ResultSetMetaData數據。 * @return */ public ResultSetMetaData getResultSetMetaData() { return this.getResultSetMetaData(this.resultSet); } /** * 執行存儲過程 * @param spName 存儲過程名稱 * @return */ public ResultSet Execute(String spName) { //對此數據庫執行一個 SQL 查詢 ResultSet resultSet = null; try { // PreparedStatement stmt = (PreparedStatement) connection.createStatement(); resultSet = statement.executeQuery(spName); } catch (Exception e) { System.out.println("execute error" + e.getMessage()); } return resultSet; } /** * 設置數據庫連接工廠,對此類的所有操作之前,必須調用該方法, * 設置數據庫連接工廠。 * @param connectionFactory 數據庫連接工廠ConnectionFactory 類對象以及 * 派生類對象。 */ public void setConnectionFactory(ConnectionFactory connectionFactory) { this.connectionFactory = connectionFactory; connection = connectionFactory.getConnection(); try { statement = connection.createStatement(); } catch (SQLException ex) { System.err.println(ex); } } public Connection getConnection() { return connection; } public java.sql.Statement getStatement() { return statement; } public javax.sql.DataSource getDataSource() { return dataSource; } } |
具體項目的數據庫訪問基類
package skydev.modules.data; public class DbObject extends DatabaseObject { // private final static String driveName = "sun.jdbc.obdc.JdbcOdbcDriver"; public DbObject() { super(new SqlServerConnectionFactory("localhost", 1433, "TheSchool", "sa","")); } public DbObject(ConnectionFactory connectionFactory) { super(connectionFactory); } } |
如:User類專門負責Users組的權限控制等,只需要簡單的代碼就可以連接并訪問數據庫了。這里具體實 現與此文章無關,只舉一兩個模塊做例子。
public class User extends DbObject { public User() { //子類也可以覆蓋基類的訪問方式,在單機調式時有用。 // super(new SqlServerConnectionFactory("localhost", 1433, "TheSchool", "sa","")); super();//調用基類的數據庫訪問代碼。 } /* 在做信息系統時為了提高客維護性,我們一般使用存儲過程返回和修改數據,在數據庫層代碼不使用 Select語句直接檢索數據,做到數據庫層代碼的最大的靈活性和可維護性。一旦發現需要修改數據庫中的 代碼,只需要修改村年初過程即可以。 下面介紹Java使用SqlServer StoreProcedure的方法。 存儲過程的參數使用“?”代替,下面的代碼有一定的代表性,存儲過程有輸入參數,輸出參數。 存儲過程的基本功能為:檢測userID和encPassword是否和數據庫存儲的一致,返回UserID,如果不一 致返回-1。 */ //測試數據庫中存儲的已經加密的密碼和用戶傳入的加密的密碼是否一致。 public boolean testPassword(int userID, byte[] encPassword) { Connection con = this.getConnection(); CallableStatement cs = null; try { cs = con.prepareCall("{?=call sp_Accounts_TestPassword(?,?)}"); cs.setInt(2, userID); cs.setBytes(3, encPassword); cs.registerOutParameter(1, Types.INTEGER); //@UserID cs.execute(); if (cs.getInt(1) == 1) { //密碼合格 return true; } else { return false; } } catch (SQLException ex) { return false; } catch (Exception e) { return false; } } } |
以上只是我在學習和工作中的一點體會,寫出來的目的使為了和大家交流,錯誤之處希望大家提出寶貴的意見,以便把該模塊的功能做得更完善一點。
延伸閱讀
文章來源于領測軟件測試網 http://www.kjueaiud.com/