📄 sqlutil.java
字号:
inQuotes = !inQuotes;
}
else if (c == '\'' && pre1 != '\\')
{
inQuotes = !inQuotes;
}
if (c == '?' && !inQuotes)
{
V.addElement(new EndPoint(lastParmEnd, i));
lastParmEnd = i + 1;
}
pre2 = pre1;
pre1 = c;
}
V.addElement(new EndPoint(lastParmEnd, statementLength));
_TemplateStrings = new byte[V.size()][];
String Encoding = null;
// if (_Conn.useUnicode())
if (true)
{
// Encoding = _Conn.getEncoding();
Encoding = "GBK";
}
for (i = 0; i < _TemplateStrings.length; i++)
{
if (Encoding == null)
{
EndPoint ep = (EndPoint) V.elementAt(i);
int end = ep.end;
int begin = ep.begin;
int len = end - begin;
byte[] buf = new byte[len];
for (int j = 0; j < len; j++)
{
buf[j] = (byte) statementAsChars[begin + j];
}
_TemplateStrings[i] = buf;
}
else
{
try
{
EndPoint ep = (EndPoint) V.elementAt(i);
int end = ep.end;
int begin = ep.begin;
int len = end - begin;
String temp = new String(statementAsChars, begin, len);
_TemplateStrings[i] = temp.getBytes(Encoding);
}
catch (java.io.UnsupportedEncodingException ue)
{
throw new CommonException(ue.getMessage());
}
}
}
_ParameterStrings = new String[_TemplateStrings.length - 1];
_ParameterStreams = new InputStream[_TemplateStrings.length - 1];
_IsStream = new boolean[_TemplateStrings.length - 1];
_IsNull = new boolean[_TemplateStrings.length - 1];
clearParameters();
for (int j = 0; j < _ParameterStrings.length; j++)
{
_IsStream[j] = false;
}
}
catch(Exception ex){
System.out.println(ex.getMessage());
throw new CommonException(ex.getMessage());
}
}
public void clearParameters() throws java.sql.SQLException
{
for (int i = 0; i < _ParameterStrings.length; i++)
{
_ParameterStrings[i] = null;
_ParameterStreams[i] = null;
_IsStream[i] = false;
_IsNull[i] = false;
}
}
/**
* Set a parameter to SQL NULL
*
* <p><B>Note:</B> You must specify the parameters SQL type (although
* MySQL ignores it)
*
* @param parameterIndex the first parameter is 1, etc...
* @param sqlType the SQL type code defined in java.sql.Types
* @exception java.sql.SQLException if a database access error occurs
*/
public void setNull(int parameterIndex, int sqlType)
throws java.sql.SQLException
{
set(parameterIndex, "null");
_IsNull[parameterIndex - 1] = true;
}
/**
* Set a parameter to a Java boolean value. The driver converts this
* to a SQL BIT value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1...
* @param x the parameter value
* @exception java.sql.SQLException if a database access error occurs
*/
public void setBoolean(int parameterIndex, boolean x)
throws java.sql.SQLException
{
set(parameterIndex, x ? "'1'" : "'0'");
}
/**
* Set a parameter to a Java byte value. The driver converts this to
* a SQL TINYINT value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1...
* @param x the parameter value
* @exception java.sql.SQLException if a database access error occurs
*/
public void setByte(int parameterIndex, byte x) throws java.sql.SQLException
{
set(parameterIndex, String.valueOf(x));
}
/**
* Set a parameter to a Java short value. The driver converts this
* to a SQL SMALLINT value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1...
* @param x the parameter value
* @exception java.sql.SQLException if a database access error occurs
*/
public void setShort(int parameterIndex, short x) throws java.sql.SQLException
{
set(parameterIndex, String.valueOf(x));
}
/**
* Set a parameter to a Java int value. The driver converts this to
* a SQL INTEGER value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1...
* @param x the parameter value
* @exception java.sql.SQLException if a database access error occurs
*/
public void setInt(int parameterIndex, int x) throws java.sql.SQLException
{
set(parameterIndex, String.valueOf(x));
}
/**
* Set a parameter to a Java long value. The driver converts this to
* a SQL BIGINT value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1...
* @param x the parameter value
* @exception java.sql.SQLException if a database access error occurs
*/
public void setLong(int parameterIndex, long x) throws java.sql.SQLException
{
set(parameterIndex, String.valueOf(x));
}
/**
* Set a parameter to a Java float value. The driver converts this
* to a SQL FLOAT value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1...
* @param x the parameter value
* @exception java.sql.SQLException if a database access error occurs
*/
public void setFloat(int parameterIndex, float x) throws java.sql.SQLException
{
set(parameterIndex, fixDecimalExponent(String.valueOf(x)));
}
/**
* Set a parameter to a Java double value. The driver converts this
* to a SQL DOUBLE value when it sends it to the database
*
* @param parameterIndex the first parameter is 1...
* @param x the parameter value
* @exception java.sql.SQLException if a database access error occurs
*/
public void setDouble(int parameterIndex, double x)
throws java.sql.SQLException
{
//set(parameterIndex, _DoubleFormatter.format(x));
set(parameterIndex, fixDecimalExponent(String.valueOf(x)));
// - Fix for large doubles by Steve Ferguson
}
/**
* Set a parameter to a java.lang.BigDecimal value. The driver
* converts this to a SQL NUMERIC value when it sends it to the
* database.
*
* @param parameterIndex the first parameter is 1...
* @param x the parameter value
* @exception java.sql.SQLException if a database access error occurs
*/
public void setBigDecimal(int parameterIndex, BigDecimal X)
throws java.sql.SQLException
{
if (X == null)
{
setNull(parameterIndex, java.sql.Types.DECIMAL);
}
else
{
set(parameterIndex, fixDecimalExponent(X.toString()));
}
}
//
// Adds '+' to decimal numbers that are positive (MySQL doesn't
// understand them otherwise
//
protected final static String fixDecimalExponent(String dString)
{
int ePos = dString.indexOf("E");
if (ePos == -1)
{
ePos = dString.indexOf("e");
}
if (ePos != -1)
{
if (dString.length() > ePos + 1)
{
char maybeMinusChar = dString.charAt(ePos + 1);
if (maybeMinusChar != '-')
{
StringBuffer buf = new StringBuffer(dString.length() + 1);
buf.append(dString.substring(0, ePos + 1));
buf.append('+');
buf.append(dString.substring(ePos + 1, dString.length()));
dString = buf.toString();
}
}
}
return dString;
}
/**
* Set a parameter to a Java String value. The driver converts this
* to a SQL VARCHAR or LONGVARCHAR value (depending on the arguments
* size relative to the driver's limits on VARCHARs) when it sends it
* to the database.
*
* @param parameterIndex the first parameter is 1...
* @param x the parameter value
* @exception java.sql.SQLException if a database access error occurs
*/
public void setString(int parameterIndex, String x)
throws java.sql.SQLException
{
// if the passed string is null, then set this column to null
if (x == null)
{
set(parameterIndex, "null");
}
else
{
StringBuffer B = new StringBuffer(x.length() * 2);
int i;
B.append('\'');
for (i = 0; i < x.length(); ++i)
{
char c = x.charAt(i);
if (c == '\\' || c == '\'' || c == '"')
{
B.append((char) '\\');
}
B.append(c);
}
B.append('\'');
set(parameterIndex, B.toString());
}
}
/**
* Set a parameter to a java.sql.Date value. The driver converts this
* to a SQL DATE value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1...
* @param x the parameter value
* @exception java.sql.SQLException if a database access error occurs
*/
public void setDate(int parameterIndex, java.sql.Date X)
throws java.sql.SQLException
{
if (X == null)
{
setNull(parameterIndex, java.sql.Types.DATE);
}
else
{
SimpleDateFormat DF = new SimpleDateFormat("''yyyy-MM-dd''");
set(parameterIndex, DF.format(X));
}
}
/**
* Set a parameter to a java.sql.Time value. The driver converts
* this to a SQL TIME value when it sends it to the database.
*
* @param parameterIndex the first parameter is 1...));
* @param x the parameter value
* @exception java.sql.SQLException if a database access error occurs
*/
public void setTime(int parameterIndex, Time X) throws java.sql.SQLException
{
if (X == null)
{
setNull(parameterIndex, java.sql.Types.TIME);
}
else
{
set(parameterIndex, "'" + X.toString() + "'");
}
}
private final void set(int paramIndex, String S)
throws CommonException {
if (paramIndex < 1 || paramIndex > _TemplateStrings.length)
{
throw new CommonException(
"Parameter index out of range ("
+ paramIndex
+ " > "
+ _TemplateStrings.length
+ ").");
}
_IsStream[paramIndex - 1] = false;
_IsNull[paramIndex - 1] = false;
_ParameterStreams[paramIndex - 1] = null;
_ParameterStrings[paramIndex - 1] = S;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -