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

📄 xmlpreparedstatement.java

📁 数据仓库工具
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    if(this.connection.getAutoCommit())
      writer = new XmlWriter(fileName,true);
    else
      writer = new XmlWriter(fileName, false);
    XmlSqlParser parser = new XmlSqlParser(this.fileName, this.connection.getAutoCommit() );
    if( binaryStreamParameters.size() != 0 )
          parser.setBinaryStreamList( binaryStreamParameters );
    try {
      parser.parse(sql);
    }
    catch(Exception e) {
      throw new SQLException("Syntax Error. " + e.getMessage());
    }
    if(parser.sqlType.equals(parser.DROP_TABLE)) {
      execute(sql);
      return null;
    }
    if(parser.sqlType.equals(parser.DELETE)) {
      executeUpdate(sql);
      return null;
    }
    if(parser.sqlType.equals(parser.UPDATE)) {
      executeUpdate(sql);
      return null;
    } else if(parser.sqlType.equals(parser.INSERT)) {
      executeUpdate(sql);
      return null;
    } else if(parser.sqlType.equals(parser.CREATE_TABLE)) {
      execute(sql);
      return null;
    } else if(parser.sqlType.equals(parser.SELECT)) {

      String fileName = connection.getPath() + connection.getExtension();
      File checkFile = new File(fileName);
      XmlReader reader;
      try {
        reader = new XmlReader(fileName);
        String[] xxx = parser.getColumnNames();
        String[] yyy = (String[]) writer.getTableProperties(parser.getTableName()).get(0);
        boolean isOK = true;
        for(int i = 0; i < xxx.length; i++) {
          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 in table " +
                parser.getTableName());
        }
      }
      catch(Exception e) {
        throw new SQLException("Error reading data file. Message was: " + e);
      }
      XmlResultSet resultSet = new XmlResultSet(this, reader,
                               parser.getTableName(), parser.getColumnNames(),
                               parser.getWhereColumnNames(), parser.getWhereColumnValues());
      resultSet.select();
      return resultSet;
    } else {
      throw new SQLException("Syntax error.Not supported sql statement.");
    }
  }

  /**
   * Insert data into XML database
   *
   * @param  sql               Description of Parameter
   * @return                   Description of the Returned Value
   * @exception  SQLException  Description of Exception
   * @since
   */
  public int executeUpdate(String sql) throws SQLException {
    DriverManager.println("XmlJdbc - XmlStatement:executeUpdate() - sql= " + sql);
    XmlSqlParser parser = new XmlSqlParser();
    if( binaryStreamParameters.size() != 0 )
          parser.setBinaryStreamList( binaryStreamParameters );
    try {
      parser.parse(sql);
    }
    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.DROP_TABLE)) {
      throw new SQLException("Not supported DROP_TABLE statement - use execute() method");
    } else if(parser.sqlType.equals(parser.INSERT)) {
      String fileName = connection.getPath() + connection.getExtension();
      XmlWriter writeXml;
      try {
        if(this.connection.getAutoCommit())
          writeXml = new XmlWriter(fileName,true);
        else
          writeXml = new XmlWriter(fileName, false);
        ArrayList properties = writeXml.getTableProperties(parser.tableName);
        //check for existance of columns
        String[] xxx = parser.getColumnNames();
        String[] yyy = (String[]) properties.get(0);
        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 in table " +
                  parser.getTableName());
          }
        }
        //check if NOTNULL columns is set
        if(!properties.get(1).toString().equalsIgnoreCase("NO CREATE TABLE")) {
          yyy = parser.getColumnNames();
          xxx = (String[]) writeXml.getTableProperties(parser.tableName).get(2);
          isOK = true;
          for(int i = 0; i < xxx.length; i++) {
            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] + "' can not be NULL.");
          }
        }
        writeXml.insert(parser.getTableName(), parser.getColumnNames(), parser.getColumnValues());
      }
      catch(Exception e) {
        throw new SQLException("Error reading data file. Message was: " + e);
      }

    } else if(parser.sqlType.equals(parser.UPDATE)) {
      String fileName = connection.getPath() + connection.getExtension();
      XmlWriter writeXml;
      try {
        if(this.connection.getAutoCommit())
          writeXml = new XmlWriter(fileName,true);
        else
          writeXml = new XmlWriter(fileName, false);
        String[] xxx = parser.getColumnNames();
        String[] yyy = (String[]) writeXml.getTableProperties(parser.tableName).get(0);
        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 in table " +
                  parser.getTableName());
          }
        }
        writeXml.update(parser.getTableName(), parser.getColumnNames(), parser.getColumnValues(),
            parser.getWhereColumnNames(), parser.getWhereColumnValues());
      }
      catch(Exception e) {
        throw new SQLException("Error reading data file. Message was: " + e);
      }

    } else if(parser.sqlType.equals(parser.DELETE)) {
      String fileName = connection.getPath() + connection.getExtension();
      XmlWriter writeXml;
      try {
        if(this.connection.getAutoCommit())
          writeXml = new XmlWriter(fileName,true);
        else
          writeXml = new XmlWriter(fileName, false);
        String[] xxx = parser.getWhereColumnNames();
        String[] yyy = (String[]) writeXml.getTableProperties(parser.tableName).get(0);
        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 in table " +
                  parser.getTableName());
          }
        }
        writeXml.delete(parser.getTableName(), parser.getWhereColumnNames(),
            parser.getWhereColumnValues());
      }
      catch(Exception e) {
        throw new SQLException("Error reading data file. Message was: " + e);
      }

    } else if(parser.sqlType.equals(parser.DROP_TABLE)) {
    }
    return 0;

  }

  /**
   * 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
  }

  /**
   *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
   */
  public boolean execute(String sql) throws SQLException {
    XmlWriter writeXml;
    if(this.connection.getAutoCommit())
      writeXml = new XmlWriter(fileName,true);
    else
      writeXml = new XmlWriter(fileName, false);
    XmlSqlParser parser = new XmlSqlParser(this.fileName, this.connection.getAutoCommit() );
    if( binaryStreamParameters.size() != 0 )
          parser.setBinaryStreamList( binaryStreamParameters );
    try {
      parser.parse(sql);
    }
    catch(Exception e) {
      throw new SQLException("Syntax Error. " + e.getMessage());
    }
    if(parser.sqlType.equals(parser.SELECT))
      throw new SQLException("Use executeQuery() for SELECT statement");
    else if(parser.sqlType.equals(parser.UPDATE))
      executeUpdate(sql);
    else if(parser.sqlType.equals(parser.INSERT))
      executeUpdate(sql);
    else if(parser.sqlType.equals(parser.DELETE))
      executeUpdate(sql);
    else if(parser.sqlType.equals(parser.CREATE_TABLE)) {
      String fileName = connection.getPath() + connection.getExtension();
      try {
        writeXml.createTable(parser.getSqlStatement(), parser.getTableName());
      }
      catch(Exception e) {
        throw new SQLException("Error reading data file. Message was: " + e);
      }
    } else if(parser.sqlType.equals(parser.DROP_TABLE)) {
      String fileName = connection.getPath() + connection.getExtension();
      try {
        writeXml.dropTable(parser.getTableName());

⌨️ 快捷键说明

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