📄 cpreparedstatement.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Smart Business Solution. The Initial
* Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
* are Copyright (C) 1999-2005 Jorg Janke.
* All parts are Copyright (C) 1999-2005 ComPiere, Inc. All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.util;
import java.io.*;
import java.math.*;
import java.net.*;
import java.rmi.*;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
import javax.sql.*;
import org.compiere.db.*;
import org.compiere.interfaces.*;
/**
* Compiere Prepared Statement
*
* @author Jorg Janke
* @version $Id: CPreparedStatement.java,v 1.22 2005/11/20 22:40:44 jjanke Exp $
*/
public class CPreparedStatement extends CStatement implements PreparedStatement
{
/**
* Prepared Statement Constructor
*
* @param resultSetType - ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.TYPE_SCROLL_SENSITIVE
* @param resultSetConcurrency - ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
* @param sql0 unconverted sql statement
* @param trxName transaction name or null
*/
public CPreparedStatement (int resultSetType, int resultSetConcurrency,
String sql0, String trxName)
{
if (sql0 == null || sql0.length() == 0)
throw new IllegalArgumentException ("sql required");
p_vo = new CStatementVO (resultSetType, resultSetConcurrency,
DB.getDatabase().convertStatement(sql0));
// Local access
if (!DB.isRemoteObjects())
{
try
{
Connection conn = null;
Trx trx = trxName == null ? null : Trx.get(trxName, true);
if (trx != null)
conn = trx.getConnection();
else
{
if (resultSetConcurrency == ResultSet.CONCUR_UPDATABLE)
conn = DB.getConnectionRW ();
else
conn = DB.getConnectionRO();
}
if (conn == null)
throw new DBException("No Connection");
p_stmt = conn.prepareStatement (p_vo.getSql(), resultSetType, resultSetConcurrency);
return;
}
catch (Exception e)
{
log.log(Level.SEVERE, p_vo.getSql(), e);
}
}
} // CPreparedStatement
/**
* Remote Constructor
* @param vo value object
*/
public CPreparedStatement (CStatementVO vo)
{
super(vo);
} // CPreparedStatement
/**
* Execute Query
* @return ResultSet or RowSet
* @throws SQLException
* @see java.sql.PreparedStatement#executeQuery()
*/
public ResultSet executeQuery () throws SQLException
{
if (p_stmt != null) // local
return ((PreparedStatement)p_stmt).executeQuery();
//
// Client -> remote sever
log.finest("server => " + p_vo + ", Remote=" + DB.isRemoteObjects());
try
{
boolean remote = DB.isRemoteObjects() && CConnection.get().isAppsServerOK(false);
if (remote && p_remoteErrors > 1)
remote = CConnection.get().isAppsServerOK(true);
if (remote)
{
Server server = CConnection.get().getServer();
if (server != null)
{
ResultSet rs = server.pstmt_getRowSet (p_vo);
p_vo.clearParameters(); // re-use of result set
if (rs == null)
log.warning("ResultSet is null - " + p_vo);
else
p_remoteErrors = 0;
return rs;
}
log.log(Level.SEVERE, "AppsServer not found");
p_remoteErrors++;
}
}
catch (Exception ex)
{
log.log(Level.SEVERE, "AppsServer error", ex);
p_remoteErrors++;
}
// Try locally
log.warning("Execute locally");
PreparedStatement pstmt = local_getPreparedStatement (false, null); // shared connection
p_vo.clearParameters(); // re-use of result set
ResultSet rs = pstmt.executeQuery();
return rs;
} // executeQuery
/**
* Execute Query
* @param sql0 unconverted SQL to execute
* @return ResultSet or RowSet
* @throws SQLException
* @see java.sql.Statement#executeQuery(String)
*/
public ResultSet executeQuery (String sql0) throws SQLException
{
// Convert SQL
p_vo.setSql(DB.getDatabase().convertStatement(sql0));
if (p_stmt != null) // local
return p_stmt.executeQuery(p_vo.getSql());
//
return executeQuery();
} // executeQuery
/**************************************************************************
* Execute Update
* @return no of updated rows
* @throws SQLException
* @see java.sql.PreparedStatement#executeUpdate()
*/
public int executeUpdate () throws SQLException
{
if (p_stmt != null)
return ((PreparedStatement)p_stmt).executeUpdate();
//
// Client -> remote sever
log.finest("server => " + p_vo + ", Remote=" + DB.isRemoteObjects());
try
{
if (DB.isRemoteObjects() && CConnection.get().isAppsServerOK(false))
{
Server server = CConnection.get().getServer();
if (server != null)
{
int result = server.stmt_executeUpdate (p_vo);
p_vo.clearParameters(); // re-use of result set
return result;
}
log.log(Level.SEVERE, "AppsServer not found");
}
}
catch (RemoteException ex)
{
log.log(Level.SEVERE, "AppsServer error", ex);
}
// Try locally
log.warning("execute locally");
PreparedStatement pstmt = local_getPreparedStatement (false, null); // shared connection
p_vo.clearParameters(); // re-use of result set
return pstmt.executeUpdate();
} // executeUpdate
/**
* Execute Update
* @param sql0 unconverted sql
* @return no of updated rows
* @throws SQLException
* @see java.sql.Statement#executeUpdate(String)
*/
public int executeUpdate (String sql0) throws SQLException
{
// Convert SQL
p_vo.setSql(DB.getDatabase().convertStatement(sql0));
if (p_stmt != null) // local
return p_stmt.executeUpdate (p_vo.getSql());
return executeUpdate();
} // executeUpdate
/**
* Method execute
* @return boolean
* @throws SQLException
* @see java.sql.PreparedStatement#execute()
*/
public boolean execute () throws SQLException
{
if (p_stmt != null)
return ((PreparedStatement)p_stmt).execute();
throw new java.lang.UnsupportedOperationException ("Method execute() not yet implemented.");
}
/**
* Method getMetaData
* @return ResultSetMetaData
* @throws SQLException
* @see java.sql.PreparedStatement#getMetaData()
*/
public ResultSetMetaData getMetaData () throws SQLException
{
if (p_stmt != null)
return ((PreparedStatement)p_stmt).getMetaData ();
else
throw new java.lang.UnsupportedOperationException ("Method getMetaData() not yet implemented.");
}
/**
* Method getParameterMetaData
* @return ParameterMetaData
* @throws SQLException
* @see java.sql.PreparedStatement#getParameterMetaData()
*/
public ParameterMetaData getParameterMetaData () throws SQLException
{
if (p_stmt != null)
return ((PreparedStatement)p_stmt).getParameterMetaData();
throw new java.lang.UnsupportedOperationException ("Method getParameterMetaData() not yet implemented.");
}
/**
* Method addBatch
* @throws SQLException
* @see java.sql.PreparedStatement#addBatch()
*/
public void addBatch () throws SQLException
{
if (p_stmt != null)
((PreparedStatement)p_stmt).addBatch ();
else
throw new java.lang.UnsupportedOperationException ("Method addBatch() not yet implemented.");
}
/**************************************************************************
* Set Null
* @param parameterIndex index
* @param sqlType type
* @throws SQLException
*/
public void setNull (int parameterIndex, int sqlType) throws SQLException
{
if (p_stmt != null)
((PreparedStatement)p_stmt).setNull (parameterIndex, sqlType);
else
p_vo.setParameter(parameterIndex, new NullParameter(sqlType));
} // setNull
/**
* Method setNull
* @param parameterIndex int
* @param sqlType int
* @param typeName String
* @throws SQLException
* @see java.sql.PreparedStatement#setNull(int, int, String)
*/
public void setNull (int parameterIndex, int sqlType, String typeName) throws SQLException
{
if (p_stmt != null)
((PreparedStatement)p_stmt).setNull (parameterIndex, sqlType);
else
p_vo.setParameter(parameterIndex, new NullParameter(sqlType));
}
/**
* Method setBoolean
* @param parameterIndex int
* @param x boolean
* @throws SQLException
* @see java.sql.PreparedStatement#setBoolean(int, boolean)
*/
public void setBoolean (int parameterIndex, boolean x) throws SQLException
{
if (p_stmt != null)
((PreparedStatement)p_stmt).setBoolean (parameterIndex, x);
else
p_vo.setParameter(parameterIndex, new Boolean(x));
}
/**
* Method setByte
* @param parameterIndex int
* @param x byte
* @throws SQLException
* @see java.sql.PreparedStatement#setByte(int, byte)
*/
public void setByte (int parameterIndex, byte x) throws SQLException
{
if (p_stmt != null)
((PreparedStatement)p_stmt).setByte (parameterIndex, x);
else
p_vo.setParameter(parameterIndex, new Byte(x));
}
/**
* Method setShort
* @param parameterIndex int
* @param x short
* @throws SQLException
* @see java.sql.PreparedStatement#setShort(int, short)
*/
public void setShort (int parameterIndex, short x) throws SQLException
{
if (p_stmt != null)
((PreparedStatement)p_stmt).setShort (parameterIndex, x);
else
p_vo.setParameter(parameterIndex, new Short(x));
}
/**
* Method setInt
* @param parameterIndex int
* @param x int
* @throws SQLException
* @see java.sql.PreparedStatement#setInt(int, int)
*/
public void setInt (int parameterIndex, int x) throws SQLException
{
if (p_stmt != null)
((PreparedStatement)p_stmt).setInt (parameterIndex, x);
else
p_vo.setParameter(parameterIndex, new Integer(x));
}
/**
* Method setLong
* @param parameterIndex int
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -