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

📄 i18npreparedstatement.java

📁 数据仓库工具
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        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("I18nJdbc - I18nStatement:executeQuery() - sql= " + sql);
		I18nSqlParser parser = new I18nSqlParser();
		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 !");
	}
   
	try
	{

				String[] xxx = parser.getColumnNames();
			  String[] yyy = connection.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)
	{
		if( I18nDriver.DEBUG )
			e.printStackTrace();
	  throw new SQLException("Error reading data file. Message was: " + e);
	}
	//load properties file
	try {
		this.properties.load(new FileInputStream(checkFile));
	} catch (Exception e) {
		throw new SQLException("Error while loading properties");
	}

		I18nResultSet resultSet = new I18nResultSet(this,
					parser.getTableName(), parser.getColumnNames(), parser.getWhereColumnNames(),
					parser.getWhereColumnValues());
	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("I18nJdbc - I18nStatement:executeUpdate() - sql= " + sql);
   I18nSqlParser parser = new I18nSqlParser();
   this.sql = sql;
   try
   {
	 parser.parse(this);
   }
   catch (Exception e)
   {
	 throw new SQLException("Syntax Error. " + e.getMessage());
   }
	 if(parser.sqlType.equals(I18nSqlParser.SELECT))
		   throw new SQLException("Not supported SELECT statement - use executeQuery method");
	 else if (parser.sqlType.equals(I18nSqlParser.CREATE_TABLE)) {
			   throw new SQLException("Not supported CREATE TABLE statement - use execute method");
	 }
	 else if (parser.sqlType.equals(I18nSqlParser.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 !");
		   }
			 try
			 {
			   String[] xxx = parser.getColumnNames();
							   String[] yyy = connection.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.");
			   }
			 }
				try {
						   String[] colValues = parser.columnValues;
						   String[] colNames = parser.columnNames;
						   String keyColumn=connection.getNameColumn();
						   String key;
						   String value;
							 if (colNames.length!=colValues.length){
										throw new Exception("Number of columns and number of values are different!");
							 }			
						   if (colNames[0].equalsIgnoreCase(keyColumn)){
							   key = colValues[0];
							   value = colValues[1];
						   }else{
							 key = colValues[1];
							 value = colValues[0];
						   }
						   this.properties.load(new FileInputStream(checkFile));
						   if (properties.containsKey(key)==true){
								   throw new Exception("Key allready exist in the property file. Key must be unique!");
						   }else{
							   this.properties.put(key,value);
							   this.properties.store(checkFile);
								 updated++;
						   }
							
					   } catch (Exception e) {
						  throw new SQLException("Error writing data to property file."+e.getMessage());
					   }
		   }
		   catch (Exception e)
		   {
		   throw new SQLException("Error writing data file. Message was: " + e);
		   }

	 }
	else if (parser.sqlType.equals(I18nSqlParser.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 !");
		   }

		   try
		   {
			   String[] xxx = parser.getColumnNames();
			   String[] yyy = connection.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.");
				 }
			   }
               
					   try {
							  String[] colValues = parser.columnValues;
							  String[] colNames = parser.columnNames;
							  String[] colWhereNames = parser.columnWhereNames;
							  String[] colWhereValues = parser.columnWhereValues;
							  String keyColumn=connection.getNameColumn();
								String valueColumn=connection.getValueColumn();
							  String key = "";
							  String value = "";
							  boolean paramsOK = true;
							  //pick up values which will be updated.This block is used if firs column is 
							  // key column
								  if (colNames[0].equalsIgnoreCase(keyColumn)){
									  if (colValues.length==1){
									   key = colValues[0];
									  }else if (colValues.length==2){
										   key = colValues[0];
										   value = colValues[1];
									  }
							   //pick up values which will be updated.This block is used if first column is 
							   //value column
								  }else if (colNames[0].equalsIgnoreCase(valueColumn)){
										   if (colValues.length==1){
											   value = colValues[0];
										   }else if (colValues.length==2){
											   value = colValues[0];
											   key = colValues[1];
										   }
								  }
								 
								  this.properties.load(new FileInputStream(checkFile));
									if (!(colWhereNames[0].equalsIgnoreCase(keyColumn) || colWhereNames[0].equalsIgnoreCase(valueColumn))){
											throw new SQLException("Error in sql statement. Wrong column name!");
									}
								   if ((!key.equalsIgnoreCase("")) && (!value.equalsIgnoreCase(""))){
									   Enumeration enum = this.properties.keys();
										   while(enum.hasMoreElements()){
												   String oldKey = enum.nextElement().toString();
												   String oldValue = this.properties.getProperty(oldKey);
												   //update key and value if this key is equal with key from where claus and also column name from where claus is equal with key column
												   //or update key and value if this value is equal with value from where claus and also column name from where claus is equal with value column
												   if ((colWhereValues[0].equalsIgnoreCase(oldKey) && keyColumn.equals(colWhereNames[0]))
														   ||(colWhereValues[0].equalsIgnoreCase(oldValue) && valueColumn.equals(colWhereNames[0]))){							
													   this.properties.remove(oldKey);
													   this.properties.put(key, value);
													   updated++;
											   }	
										   }
								    	
									   }else if (!value.equalsIgnoreCase("")){
							   	
										   Enumeration enum = this.properties.keys();
											   while(enum.hasMoreElements()){
												   String oldKey = enum.nextElement().toString();
												   String oldValue = this.properties.getProperty(oldKey); 
												   //update value if this key is equal with key from where claus and also column name from where claus is equal with key column
												   //or update value if this value is equal with value from where claus and also column name from where claus is equal with value column                                             
													   if ((colWhereValues[0].equalsIgnoreCase(oldKey) && keyColumn.equals(colWhereNames[0]))
															||(colWhereValues[0].equalsIgnoreCase(oldValue) && valueColumn.equals(colWhereNames[0]))){
														   this.properties.put(oldKey,value);
														   updated++;
													   }
											   } 
								   }else if (!key.equalsIgnoreCase("")){
										
											   Enumeration enum = this.properties.keys();
											   while(enum.hasMoreElements()){
												   String oldKey = enum.nextElement().toString();
												   String oldValue = this.properties.getProperty(oldKey);
												   //update key if this key is equal with key from where claus and also column name from where claus is equal with key column
												   //or update key if this value is equal with value from where claus and also column name from where claus is equal with value column
												   if ((colWhereValues[0].equalsIgnoreCase(oldKey) && keyColumn.equals(colWhereNames[0]))
															   ||(colWhereValues[0].equalsIgnoreCase(oldValue) && valueColumn.equals(colWhereNames[0]))){
													   this.properties.remove(oldKey);
													   this.properties.put(key, oldValue);
													   updated++;
												   }	
											   } 
							
								   }
							  
						  this.properties.store(checkFile);
						   //}
						  } catch (Exception e) {
							 throw new SQLException("Error writing data to property file."+e.getMessage());
						  }
									
		   }
		   catch (Exception e)
		   {
		   throw new SQLException("Error writing data file. Message was: " + e);
		   }

	   }else if (parser.sqlType.equals(I18nSqlParser.DELETE)){
					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 !");
					}
					   try
						   {
							 
////						   try {
							   boolean deleteAll=false;
							   String[] colWhereNames = parser.columnWhereNames;
							   String[] colWhereValues = parser.columnWhereValues;
							 	 String keyColumn=connection.getNameColumn();
							   String valueColumn=connection.getValueColumn();
							   this.properties.load(new FileInputStream(checkFile));
								 if (!(colWhereNames[0].equalsIgnoreCase(keyColumn) || colWhereNames[0].equalsIgnoreCase(valueColumn))){
											throw new SQLException("Error in sql statement. Wrong column name!");
								 }
							   Enumeration enum = this.properties.keys();
							   if (colWhereNames.length==0){
								   deleteAll=true;
							   }
							   if  (!deleteAll){
								   while(enum.hasMoreElements()){
									   String oldKey = enum.nextElement().toString();
									   String oldValue = this.properties.getProperty(oldKey);
											                
											   if ((colWhereValues[0].equalsIgnoreCase(oldKey) && keyColumn.equals(colWhereNames[0]))
													||(colWhereValues[0].equalsIgnoreCase(oldValue) && valueColumn.equals(colWhereNames[0]))){
												   this.properties.remove(oldKey);
												   this.properties.store(checkFile);
												   updated++;				
											   }
											
								   }
							   }else{
									   this.properties.clear();
									   this.properties.store(checkFile);	
							   } 
						  } catch (Exception e) {
							 throw new SQLException("Error deleting data from property file."+e.getMessage());
					   }

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

⌨️ 快捷键说明

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