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

📄 csvstatement.java

📁 数据仓库工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            isOK=false;
        }
       if(!isOK)
        throw new SQLException("Column '" + xxx[i] + "' not found.");
      }
      }
    }
    catch (Exception e)
    {
    	if( CsvDriver.DEBUG )
    		e.printStackTrace();
      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();
    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)
    		{
      		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 {
        if(this.writeCsv!=null)
        this.writeCsv.close();
//        this.writeCsv.output.close();
      }
      catch(Exception e) {
        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
     */
    public boolean execute(String sql) throws SQLException
  {

    CsvSqlParser parser = new CsvSqlParser();
    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.INSERT))
	  		executeUpdate(sql);
 	  else if (parser.sqlType.equals(parser.CREATE_TABLE)) {
	  	   String fileName = connection.getPath() + parser.getTableName() + connection.getExtension();
    		File checkFile = new File(fileName);

    		if (checkFile.exists())
                    {
                      throw new SQLException("Data file '" + fileName + "'already exists  !");
                    }

              //    	   CsvWriter writeCsv;
                    try {
                      if (connection.getAutoCommit())
                        writeCsv = new CsvWriter(
                            fileName,
                            connection.getSeperator(),
                            connection.getExtension(),
                            connection.getMaxFileSize()
                            );
                      else {
                        writeCsv.setFileName(fileName);
                        writeCsv.fillTableColumnNames();
                }

      		writeCsv.createTable(parser.columnNames, fileName);
    		}
    		catch (Exception e)
    		{
      		throw new SQLException("Error reading data file. Message was: " + e);
    		}
    	}
		return true;

  }



    /**
     *Adds a feature to the Batch attribute of the CsvStatement object
     *
     * @param  p0                The feature to be added to the Batch attribute
     * @exception  SQLException  Description of Exception
     * @since
     */
    public void addBatch(String p0) throws SQLException
    {
        throw new SQLException("Not Supported !");
    }


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


    /**
     *Description of the Method
     *
     * @return                   Description of the Returned Value
     * @exception  SQLException  Description of Exception
     * @since
     */
    public int[] executeBatch() throws SQLException
    {
        throw new SQLException("Not Supported !");
    }

    //---------------------------------------------------------------------
    // JDBC 3.0
    //---------------------------------------------------------------------

    public boolean getMoreResults(int current) throws SQLException {
        throw new UnsupportedOperationException("Statement.getMoreResults(int) unsupported");
    }

    public ResultSet getGeneratedKeys() throws SQLException {
        throw new UnsupportedOperationException("Statement.getGeneratedKeys() unsupported");
    }

    public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
        throw new UnsupportedOperationException("Statement.executeUpdate(String,int) unsupported");
    }

    public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
        throw new UnsupportedOperationException("Statement.executeUpdate(String,int[]) unsupported");
    }

    public int executeUpdate(String sql, String[] columnNames) throws SQLException {
        throw new UnsupportedOperationException("Statement.executeUpdate(String,String[]) unsupported");
    }

    public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
        throw new UnsupportedOperationException("Statement.execute(String,int) unsupported");
    }

    public boolean execute(String sql, int[] columnIndexes) throws SQLException {
        throw new UnsupportedOperationException("Statement.execute(String,int[]) unsupported");
    }

    public boolean execute(String sql, String[] columnNames) throws SQLException {
        throw new UnsupportedOperationException("Statement.execute(String,String[]) unsupported");
    }

    public int getResultSetHoldability() throws SQLException {
        throw new UnsupportedOperationException("Statement.getResultSetHoldability() unsupported");
    }

    public String getSqlStatement() {
      return sql;
    }
}

⌨️ 快捷键说明

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