WEB开发网
开发学院软件开发Java 客串 java 开发:数据库访问的流程简化 阅读

客串 java 开发:数据库访问的流程简化

 2009-10-13 00:00:00 来源:WEB开发网   
核心提示: 同时,实现一个链接传统数据库的辅助类:packagequeen.data;importjava.io.IOException;importjava.sql.Connection;importjava.sql.Statement;importjava.sql.ResultSet;importjav

同时,实现一个链接传统数据库的辅助类:

package queen.data; 
 
import java.io.IOException; 
import java.sql.Connection; 
import java.sql.Statement; 
import java.sql.ResultSet; 
import java.sql.DriverManager; 
import java.util.Properties; 
 
/* 
 * 数据库访问器 
 * 
 * @author Howard Queen 
 * @version 1.0.1, 2010/10/12 
 * */ 
public final class DbExecutor implements IExecutor { 
   
  static{ 
    try { 
      Class.forName("com.mysql.jdbc.Driver"); 
    } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
    } 
  } 
 
  private String _connectionString; 
  private String _userName, _password; 
  public String getConnectionString() { 
    return _connectionString; 
  } 
  public void setConnectionString(String value) throws Exception { 
    throw new Exception("请使用同名重载方法 setConnectionString(String connectionString, 

                 String userName, String password) 设置链接参数!"); 
  } 
   
  public void setConnection(String connectionString, String userName, String password){ 
    _connectionString = connectionString; 
    _userName = userName; 
    _password = password; 
  } 
   
  public void setConnectionFromProperties(String fileName, String connectionStringPropertyName, 

        String userNamePropertyName, String passwordPropertyName) throws IOException{ 
    Properties p = new Properties(); 
    p.load(DbExecutor.class.getResourceAsStream(fileName)); 
    String connectionString = p.getProperty(connectionStringPropertyName); 
    String userName = p.getProperty(userNamePropertyName); 
    String password = p.getProperty(passwordPropertyName); 
    setConnection(connectionString, userName, password); 
  } 
   
  public String getUserName(){ 
    return _userName; 
  } 
   
  public String getPassword(){ 
    return _password; 
  } 
     
  private int _timeout; 
  public int getTimeout() { 
    return _timeout; 
  } 
  public void setTimeout(int value) { 
    _timeout = value; 
  } 
   
  public int executeNonQuery(String statement) throws Exception{ 
    Connection con = null; 
    Statement stm = null; 
    try{ 
      con = DriverManager.getConnection(getConnectionString(), getUserName(), getPassword()); 
      stm = con.createStatement(); 
      return stm.executeUpdate(statement); 
    } 
    catch(Exception E){ 
      throw E; 
    } 
    finally{ 
      if(stm != null){ 
        stm.close(); 
      } 
      if(con != null){ 
        con.close(); 
      } 
    } 
  } 
 
  public QueryResult executeQuery(String statement) throws Exception { 
    Connection con = DriverManager.getConnection(getConnectionString(), getUserName(), getPassword()); 
    Statement stm = con.createStatement(); 
    return new QueryResult(con, stm, stm.executeQuery(statement)); 
  } 
 
  public Object executeScale(String statement) throws Exception { 
    Connection con = null; 
    Statement stm = null; 
    ResultSet rs = null; 
    try{ 
      con = DriverManager.getConnection(getConnectionString(), getUserName(), getPassword()); 
      stm = con.createStatement(); 
      rs = stm.executeQuery(statement); 
      if(rs.next()){ 
        return rs.getObject(0); 
      } 
      return null; 
    } 
    catch(Exception E){ 
      throw E; 
    } 
    finally{     
      if(rs != null){ 
        rs.close(); 
      } 
      if(stm != null){ 
        stm.close(); 
      } 
      if(con != null){ 
        con.close(); 
      } 
    } 
  } 
   
} 

上一页  1 2 3 4  下一页

Tags:客串 java 开发

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接