📄 i18nstatement.java
字号:
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];
}
}
//if number of columns for update is (1 or 2) then paramsOK=true
this.properties.load(new FileInputStream(checkFile));
//column name in where claus is wrong
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
{
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>
* 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(); ) {
I18nResultSet resultSet = (I18nResultSet) i.nextElement();
resultSet.close();
}
}
/**
*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
{
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.INSERT))
executeUpdate(sql);
else if (parser.sqlType.equals(I18nSqlParser.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 !");
}
try {
checkFile.createNewFile();
}
catch (Exception e)
{
throw new SQLException("Error reading data file. Message was: " + e);
}
}
return true;
}
/**
*Adds a feature to the Batch attribute of the I18nStatement 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");
}
/**
* This method returns string which represents sql statement
* @return string
*/
public String getSqlStatement() {
return sql;
}
/**
* This method returns properties object
* @return properties object
*/
public I18nProperties getProperties() {
return properties;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -