⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csvpreparedstatement.java

📁 数据仓库工具
💻 JAVA
📖 第 1 页 / 共 3 页
字号:


    /**
     *Gets the resultSetConcurrency attribute of the CsvStatement object
     *
     * @return                   The resultSetConcurrency value
     * @exception  SQLException  Description of Exception
     * @since
     */
    public int getResultSetConcurrency() throws SQLException
    {
        throw new SQLException("Not Supported !");
    }


    /**
     *Gets the resultSetType attribute of the CsvStatement object
     *
     * @return                   The resultSetType value
     * @exception  SQLException  Description of Exception
     * @since
     */
    public int getResultSetType() throws SQLException
    {
        throw new SQLException("Not Supported !");
    }


    /**
     *Gets the connection attribute of the CsvStatement object
     *
     * @return                   The connection value
     * @exception  SQLException  Description of Exception
     * @since
     */
    public Connection getConnection() throws SQLException
    {
        return connection;
    }


    /**
     *Description of the Method
     *
     * @param  sql               Description of Parameter
     * @return                   Description of the Returned Value
     * @exception  SQLException  Description of Exception
     * @since
     */
    public ResultSet executeQuery(String sql) throws SQLException
    {
        DriverManager.println("CsvJdbc - CsvStatement:executeQuery() - sql= " + sql);
        CsvSqlParser parser = new CsvSqlParser();
        if( binaryStreamParameters.size() != 0 )
          parser.setBinaryStreamList( binaryStreamParameters );

        this.sql = sql;
        try
            {
                parser.parse(this);
            }
        catch (Exception e)
            {
                throw new SQLException("Syntax Error. " + e.getMessage());
            }

    String fileName = connection.getPath() + parser.getTableName() + connection.getExtension();
    File checkFile = new File(fileName);
    if (!checkFile.exists())
    {
      throw new SQLException("Cannot open data file '" + fileName + "'  !");
    }

    if (!checkFile.canRead())
    {
      throw new SQLException("Data file '" + fileName + "'  not readable !");
    }
    CsvReader reader;
    try
    {
      reader = new CsvReader(fileName,
                             connection.getSeperator(),
                             connection.isSuppressHeaders(),
                             connection.getCharset(),
                             connection.getExtension(),
                             connection.getLineBreakEscape(),
//                             connection.getDoubleQuotesEscape(),
                             connection.getCarriageReturnEscape()
                             );
      String[] xxx = parser.getColumnNames();
      String[] yyy = reader.getColumnNames();
      boolean isOK = true;
      for(int i=0; i< xxx.length; i++) {
      if(!xxx[i].endsWith("*")) {
       out:
      for(int j=0; j< yyy.length; j++) {
         if(xxx[i].equalsIgnoreCase( yyy[j] )) {
            isOK=true;
            break out;
          }
          else
            isOK=false;
        }
       if(!isOK)
        throw new SQLException("Column '" + xxx[i] + "' not found.");
      }
      }
    }
    catch (Exception e)
    {
      throw new SQLException("Error reading data file. Message was: " + e);
    }
    CsvResultSet resultSet = new CsvResultSet(this, reader,
            parser.getTableName(), parser.getColumnNames(), parser.getWhereColumnNames(),
            parser.getWhereColumnValues(), reader.getColumnTypes());
    resultSets.add(resultSet);
    return resultSet;
  }



    /**
     *Description of the Method
     *
     * @param  sql               Description of Parameter
     * @return                   Description of the Returned Value
     * @exception  SQLException  Description of Exception
     * @since
     */
    public int executeUpdate(String sql) throws SQLException
    {
    int updated=0;
    DriverManager.println("CsvJdbc - CsvStatement:executeUpdate() - sql= " + sql);
    CsvSqlParser parser = new CsvSqlParser();
    if( binaryStreamParameters.size() != 0 )
          parser.setBinaryStreamList( binaryStreamParameters );
    this.sql = sql;
    try
    {
      parser.parse(this);
    }
    catch (Exception e)
    {
      throw new SQLException("Syntax Error. " + e.getMessage());
    }
      if(parser.sqlType.equals(parser.SELECT))
              throw new SQLException("Not supported SELECT statement - use executeQuery method");
      else if (parser.sqlType.equals(parser.CREATE_TABLE)) {
                  throw new SQLException("Not supported CREATE TABLE statement - use execute method");
      }
      else if (parser.sqlType.equals(parser.INSERT)) {
            String fileName = connection.getPath() + parser.getTableName() + connection.getExtension();
            File checkFile = new File(fileName);

            if (!checkFile.exists())
            {
              throw new SQLException("Cannot open data file '" + fileName + "'  !");
            }

            if (!checkFile.canWrite())
            {
              throw new SQLException("Data file '" + fileName + "'  is read only !");
            }
//           CsvWriter writeCsv;
            try
            {
              if(connection.getAutoCommit())
                writeCsv = new CsvWriter(
                  fileName,
                  connection.getSeperator(),
                  connection.getExtension(),
                  connection.getMaxFileSize()
                  );
              else {
                writeCsv.setFileName(fileName);
                writeCsv.fillTableColumnNames();
              }

                String[] xxx = parser.getColumnNames();
                String[] yyy = writeCsv.getColumnNames();
                boolean isOK = true;
                for(int i=0; i< xxx.length; i++) {
                  if(!xxx[i].endsWith("*")) {
                  out:
                  for(int j=0; j< yyy.length; j++) {
                    if(xxx[i].equalsIgnoreCase( yyy[j] )) {
                      isOK=true;
                      break out;
                    }
                  else
                    isOK=false;
                  }
                if(!isOK)
                  throw new SQLException("Column '" + xxx[i] + "' not found.");
                }
              }
              writeCsv.newLine(parser.columnNames, parser.columnValues);

            }
            catch (Exception e)
            {
              e.printStackTrace();
              throw new SQLException("Error reading data file. Message was: " + e);
            }

      }
      else if (parser.sqlType.equals(parser.UPDATE)) {

            String fileName = connection.getPath() + parser.getTableName() + connection.getExtension();
            File checkFile = new File(fileName);

            if (!checkFile.exists())
            {
              throw new SQLException("Cannot open data file '" + fileName + "'  !");
            }

            if (!checkFile.canWrite())
            {
              throw new SQLException("Data file '" + fileName + "'  is read only !");
            }
//           CsvWriter writeCsv;
            try
            {
              if(connection.getAutoCommit())
                writeCsv = new CsvWriter(
              fileName,
              connection.getSeperator(),
              connection.getExtension(),
              connection.getMaxFileSize()
              );
              else{
               writeCsv.setFileName(fileName);
               writeCsv.fillTableColumnNames();
              }

                String[] xxx = parser.getColumnNames();
                String[] yyy = writeCsv.getColumnNames();
                boolean isOK = true;
                for(int i=0; i< xxx.length; i++) {
                  if(!xxx[i].endsWith("*")) {
                    out:
                  for(int j=0; j< yyy.length; j++) {
                    if(xxx[i].equalsIgnoreCase( yyy[j] )) {
                      isOK=true;
                      break out;
                    }
                  else
                    isOK=false;
                }
                if(!isOK)
                  throw new SQLException("Column '" + xxx[i] + "' not found.");
                  }
                }
                if(!writeCsv.updateFields(parser.columnNames, parser.columnValues, parser.columnWhereNames, parser.columnWhereValues))
                  updated = -1;
            }
            catch (Exception e)
            {
              throw new SQLException("Error reading data file. Message was: " + e);
            }

        }
      return updated;

  }


    /**
     * Releases this <code>Statement</code> object's database
     * and JDBC resources immediately instead of waiting for
     * this to happen when it is automatically closed.
     * It is generally good practice to release resources as soon as
     * you are finished with them to avoid tying up database
     * resources.
     * <P>
     * Calling the method <code>close</code> on a <code>Statement</code>
     * object that is already closed has no effect.
     * <P>
     * <B>Note:</B> A <code>Statement</code> object is automatically closed
     * when it is garbage collected. When a <code>Statement</code> object is
     * closed, its current <code>ResultSet</code> object, if one exists, is
     * also closed.
     *
     * @exception SQLException if a database access error occurs
     */
    public void close() throws SQLException {
      // close all result sets
      for(Enumeration i = resultSets.elements(); i.hasMoreElements(); ) {
        CsvResultSet resultSet = (CsvResultSet) i.nextElement();
        resultSet.close();
      }
      try {
        this.writeCsv.close();
      }
      catch(Exception e) {
        e.printStackTrace();
        throw new SQLException(e.getMessage());
      }
    }

    /**
     *Description of the Method
     *
     * @exception  SQLException  Description of Exception
     * @since
     */
    public void cancel() throws SQLException
    {
        throw new SQLException("Not Supported !");
    }


    /**
     *Description of the Method
     *
     * @exception  SQLException  Description of Exception
     * @since
     */
    public void clearWarnings() throws SQLException
    {
        throw new SQLException("Not Supported !");
    }


    /**
     *Description of the Method
     *
     * @param  sql               Description of Parameter
     * @return                   Description of the Returned Value
     * @exception  SQLException  Description of Exception
     * @since

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -