package com.dicsi;
import java.sql.*;
import java.util.*;
import java.text.*;public class TouchSqlSelect {  private Hashtable _records[];                     //保存结果集的散列表
  private int _columns=0;                           //列数
  private int _current=0;                           //当前记录号
  private int _max=0;                               //最大记录数
  private String _colInfo[][];                      //列信息
  private Connection _conn;                         //数据库连接
  private TouchConnPool _connPool;                    //连接池实例
  private String _poolName="touch";                  //缺省连接池名称
  private String _databaseName="";                   //缺省数据库名称
  private String _errorMessage;                     //出错信息
  private DecimalFormat money = new DecimalFormat("0.00");   //格式输出
  public TouchSqlSelect() {
     _connPool=TouchConnPool.getInstance();           //创建数据库连接池实例
  }  private void getConnection () {
     _conn=_connPool.getConnection(_poolName);     //按连接池名称取得连接
     if ( !_databaseName.equals("") &  _databaseName!=null  ) {
          try {
              _conn.setCatalog(_databaseName);
          }
          catch (Exception e ) {
              _errorMessage="设置数据库出错:"+e;
          }
     }  }  public void setPoolName(String poolName) {
     _poolName=poolName;                           //设置连接池名称
  }  public void setDatabase(String databaseName) {
     _databaseName=databaseName;                   //设置数据库
  }  public int sqlSelect(String sql) {
      int index=0;
      getConnection();
      if ( _conn == null ) {
          _errorMessage=_connPool.getError();
          this._max= -1;
      }
      try {
            Statement sqlStatement = this._conn.createStatement();
            ResultSet results = sqlStatement.executeQuery(sql);
            ResultSetMetaData resultsMeta = results.getMetaData();
            this._columns = resultsMeta.getColumnCount();
            _colInfo=new String[_columns][4];
        for (int i=0;i<_columns;i++) {
      _colInfo[i][0]=resultsMeta.getColumnName(i+1);                            //列名
       _colInfo[i][1]=resultsMeta.getColumnTypeName(i+1);                        //类型
       _colInfo[i][2]=Integer.toString(resultsMeta.getColumnDisplaySize(i+1));   //长度
      _colInfo[i][3]="D";                                                       //排序标志
            }
            String value;
            _records = new Hashtable[this._columns];
            while (results.next()) {
               index++;
               for (int i=0;i<_columns;i++) {
                    if (index == 1) {
                        _records[i]=new Hashtable();
                    }
                    value=results.getString(i+1);
                    if (value == null) value="";
     _records[i].put(new Integer(index),value.trim());
               }            }
            results.close();
            _connPool.freeConnection(_poolName , _conn);   //将连接返回连接池
            this._max = index;
            if (_max >= 1) _current=0;                     //初始结果集当前记录      }
      catch(Exception e) {
            this._max=-1;
            System.out.println("===Exceptionlhj3===");
            _errorMessage="TouchSqlSelect出错:"+e+",sql语句:"+sql;
      }
      return _max;
  }public boolean next() {
      if (this._current == _max)
         return false;
      else {
  this._current++;
  return true;
      }
  }
public boolean prev() {
      if (this._current < 2 )
         return false;
      else {
  this._current--;
  return true;
      }
  }
public void first() {
      this._current=1;
  }
public void last() {
      this._current=_max;
  }
public int getRow() {
      return this._current;
  }

解决方案 »

  1.   

    public boolean setRow(int Row) {
          if ((Row<0) || (Row>_max)) {
             return false;
          }
          else {
             this._current=Row;
             return true;
          }
      }
    public int rowCount() {
          return this._max;
      }
    public int columnCount() {
          return this._columns;
      }
    public String getColumnName(int index) {
        if ((index<1) || (index>_columns))
             return "";
        else
             return _colInfo[index-1][0];
      }
    public int getColumnIndex(String colName) {
        for (int i=0;i<_columns;i++) {
          if ( colName.equals(_colInfo[i][0]) ) {
               return i;
          }
        }
        return -1;
      }
    public String getColumnType(int index) {
        if ((index<1) || (index>_columns))
             return "";
        else
             return _colInfo[index-1][1];
      }
    public int getColumnSize(int index) {
        if ((index<1) || (index>_columns))
             return 0;
        else {
             try {
               return Integer.parseInt(_colInfo[index-1][2]);
             } catch (NumberFormatException e) {
               return 0;
             }
        }
      }
    public String[][] getColInfo() {
        return _colInfo;
      }
    public String getItem(int Row,int Column) {
          if ((Row<1) || (Row>_max)) {
             _errorMessage="TouchSqlSelect出错:行号"+Row+"越界,最大行号"+_max+"越界行号"+Row;
               return null;
          }
          else if ((Column<1) || (Column>this._columns)) {
             _errorMessage="TouchSqlSelect出错:未找到第"+Column+"列,共有"+this._columns+"列";
               return null;
          }
          else {
               try {
                 String colType=_colInfo[Column-1][1];
                 if ( colType.equals("char") || colType.equals("varchar") )
                      return ((String)_records[Column - 1].get(new Integer(Row))).trim();
                 else
                      return (String)_records[Column - 1].get(new Integer(Row));
                } catch (Exception e) {
                      _errorMessage="取值错误:"+e;
                      return null;
                }      }
      }
     public String getItem(int Row,String ColumnName) {
          int index=getColumnIndex(ColumnName);
          if (index<0) {
              _errorMessage="TouchSqlSelect出错:未找到"+ColumnName+"列";
              return null;
          }
          return getItem(Row,index+1);
      }
    public String getItem(int Row,int Column, String ByteCode) {
         String value=getItem(Row,Column);
         if (value!=null)
             return changeBytes(value,ByteCode);
         else
             return null;
      }
     public String getItem(int Row,String ColumnName, String ByteCode) {
         String value=getItem(Row,ColumnName);
         if (value!=null)
             return changeBytes(value,ByteCode);
         else
             return null;
      }
    //取SQL语句返回值
      public String[] getValue(String sql, int valueNumber) {
          String[] value=new String[valueNumber+1];
          getConnection();
          if ( _conn == null ) {
              _errorMessage=_connPool.getError();
              return value;
          }
          try {
               int index=0;
                Statement sqlStatement = this._conn.createStatement();
                ResultSet results = sqlStatement.executeQuery(sql);
                while (results.next()) {
                   index++;
                   for (int i=1;i<=valueNumber;i++) {
                        String data=results.getString(i);
                        if (data==null)
                            value[i]="";
                        else
                            value[i]=data.trim();
                   }
                }
                value[0]=Integer.toString(index);
                results.close();
                _connPool.freeConnection(_poolName, _conn);   //将连接返回连接池
                }
          catch(Exception e) {
                _errorMessage="TouchSqlSelect出错:"+e;
          }
          return value;
      }
    public String getError() {
        return _errorMessage;
      }
    //转换字符集
      public String changeBytes ( String value , String  bytecodeFrom, String bytecodeTo) {
         if (bytecodeFrom=="") bytecodeFrom="ISO-8859-1";
         if (bytecodeTo=="") bytecodeTo="GB2312";
         try {
           return new String(value.getBytes(bytecodeFrom),bytecodeTo);
         }
         catch (Exception e ) {
           _errorMessage=value+"字符集转换错误,"+e;
          return "";
         }
      }
     //转换字符集
      public String changeBytes ( String value , String  bytecodeTo) {
         if (bytecodeTo=="") bytecodeTo="GB2312";
         try {
           return new String(value.getBytes("ISO-8859-1"),bytecodeTo);
         }
         catch (Exception e ) {
           _errorMessage=value+"字符集转换错误,"+e;
          return "";
         }
      }
    //转换字符集
      public String changeBytes ( String value ) {
         String bytecode="GB2312";
         try {
           return new String(value.getBytes("ISO-8859-1"),bytecode);
         }
         catch (Exception e ) {
           _errorMessage=value+"字符集转换错误,"+e;
          return "";
         }
      }}
      

  2.   

    头晕啊,但是我用JNDI,在WEBLOGIC ADMIN配了一下,然后走JDBC ,try catch finally就没什么问题啊
      

  3.   

    comfig.xml文件如下:请问有什么错误吗?
    <?xml version="1.0" encoding="UTF-8"?>
    <!--Last updated on: Tue Jan 11 14:24:17 CST 2005, If your domain is active, please do not edit the config.xml file. Any changes made to that file while the domain is active will not have any effect on the domain's configuration and are likely to be lost. If your domain is inactive, you may edit this file with an XML editor. If you do so, please refer to the BEA Weblogic Server Configuration Reference  documentation available from http://edocs.bea.com/wls/docs70/config_xml.  In general, we recommend that changes to your configuration file be made through the Administration Console.-->
    <Domain ConfigurationVersion="7.0.4.0" Name="mydomain">
        <Application Deployed="true" Name="DefaultWebApp"
            Path=".\applications" StagedTargets="" TwoPhase="false">
            <WebAppComponent Name="DefaultWebApp" Targets="myserver" URI="DefaultWebApp"/>
        </Application>
        <Application Deployed="true" Name="certificate"
            Path=".\applications" StagedTargets="" TwoPhase="false">
            <WebAppComponent Name="certificate" Targets="myserver" URI="certificate.war"/>
        </Application>
        <ApplicationManager Name="mydomain"/>
        <EmbeddedLDAP
            Credential="{3DES}c1Z+smDcj8dR3lQOYhFMYogIbneMo0691Uf+sKLx5x0=" Name="mydomain"/>
        <FileRealm Name="wl_default_file_realm"/>
        <JDBCConnectionPool DriverName="oracle.jdbc.driver.OracleDriver"
            InitialCapacity="10" MaxCapacity="30" Name="mypool"
            Properties="user=psmis;password=psmis123;dll=ocijdbc9;protocol=thin"
            Targets="myserver" URL="jdbc:oracle:thin:@192.168.0.5:1521:ZAOZMIS"/>
        <JDBCDataSource JNDIName="OraJndi" Name="mydata" PoolName="mypool" Targets="myserver"/>
        <JTA Name="mydomain"/>
        <Log FileName=".\wl-domain.log" Name="mydomain"/>
        <PasswordPolicy Name="wl_default_password_policy"/>
        <Realm FileRealm="wl_default_file_realm" Name="wl_default_realm"/>
        <SNMPAgent Name="mydomain"/>
        <Security GuestDisabled="false" Name="mydomain"
            PasswordPolicy="wl_default_password_policy"
            Realm="wl_default_realm" RealmSetup="true"/>
        <SecurityConfiguration
            Credential="{3DES}w8iORm+8AcasP9xgJcTk44Yfu5fnBM/LxL5pmkJhOvYwaAhTFKucq7lXczQjLEiL45fla+cWUqNdlvuHcR3U7CGM/VyhTX2T" Name="mydomain"/>
        <Server ListenPort="7001" Name="myserver" NativeIOEnabled="true" ServerVersion="7.0.4.0">
            <COM Name="myserver"/>
            <ExecuteQueue Name="default" ThreadCount="15"/>
            <IIOP Name="myserver"/>
            <JTAMigratableTarget Cluster="" Name="myserver" UserPreferredServer="myserver"/>
            <JTARecoveryService Name="myserver"/>
            <KernelDebug Name="myserver"/>
            <Log FileName=".\myserver\myserver.log" Name="myserver"/>
            <SSL Enabled="true" HostnameVerificationIgnored="true"
                ListenPort="7002" Name="myserver"
                ServerCertificateFileName="democert.pem"
                ServerPrivateKeyAlias="demokey" ServerPrivateKeyPassPhrase="{3DES}uwtCk9CjBWwNMYQIcIj4+A=="/>
            <ServerDebug Name="myserver"/>
            <ServerStart Name="myserver"/>
            <WebServer DefaultWebApp="DefaultWebApp"
                LogFileName="myserver\access.log" LoggingEnabled="true" Name="myserver"/>
        </Server>
    </Domain>