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

📄 preparedstatement.java

📁 SSD9 练习9
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    }    public void setObject(int parameterIndex, Object X, int targetSqlType) throws java.sql.SQLException    {	setObject(parameterIndex, X, targetSqlType, 0);    }    public void setObject(int parameterIndex, Object X) throws java.sql.SQLException    {	if (X == null) {	    setNull(parameterIndex, java.sql.Types.OTHER);	}	else {	    if (X instanceof String)		setString(parameterIndex, (String)X);	    else if (X instanceof BigDecimal)		setBigDecimal(parameterIndex, (BigDecimal)X);	    else if (X instanceof Integer)		setInt(parameterIndex, ((Integer)X).intValue());	    else if (X instanceof Long)		setLong(parameterIndex, ((Long)X).longValue());	    else if (X instanceof Float)		setFloat(parameterIndex, ((Float)X).floatValue());	    else if (X instanceof Double)		setDouble(parameterIndex, ((Double)X).doubleValue());	    else if (X instanceof byte[])		setBytes(parameterIndex, (byte[])X);	    else if (X instanceof java.sql.Date)		setDate(parameterIndex, (java.sql.Date)X);	    else if (X instanceof Time)		setTime(parameterIndex, (Time)X);	    else if (X instanceof Timestamp)		setTimestamp(parameterIndex, (Timestamp)X);	    else if (X instanceof Boolean)		setBoolean(parameterIndex, ((Boolean)X).booleanValue());	    else {		try {		    ByteArrayOutputStream BytesOut = new ByteArrayOutputStream();		    ObjectOutputStream ObjectOut = new ObjectOutputStream(BytesOut);		    ObjectOut.writeObject(X);		    ObjectOut.flush();		    ObjectOut.close();		    BytesOut.flush();		    BytesOut.close();		    		    byte[] buf = BytesOut.toByteArray();		    ByteArrayInputStream BytesIn = new ByteArrayInputStream(buf);		    setBinaryStream(parameterIndex, BytesIn, -1);		}		catch (Exception E) {		    throw new java.sql.SQLException("Invalid argument value: " + E.getClass().getName(), "S1009");		}	    }	}    }        /**     * Some prepared statements return multiple results; the execute method     * handles these complex statements as well as the simpler form of      * statements handled by executeQuery and executeUpdate     *     * @return true if the next result is a ResultSet; false if it is an     *      update count or there are no more results     * @exception java.sql.SQLException if a database access error occurs     */    public boolean execute() throws java.sql.SQLException    {	boolean do_escape_processing = _escapeProcessing;	_escapeProcessing = false;		Buffer Packet = new Buffer(MysqlIO.getMaxBuf());	Packet.writeByte((byte)MysqlDefs.QUERY);		            	String Encoding = null;	if (_Conn.useUnicode()) {	    Encoding = _Conn.getEncoding();	}	try {	    for (int i = 0 ; i < _ParameterStrings.length ; ++i) {		if (_ParameterStrings[i] == null && 		    (_IsStream[i] && _ParameterStreams[i] == null)) {		    throw new java.sql.SQLException("No value specified for parameter " + (i + 1));		}		if (Encoding != null) {		    Packet.writeStringNoNull(_TemplateStrings[i], Encoding);		}		else {		    Packet.writeStringNoNull(_TemplateStrings[i]);		}		    		if (_IsStream[i]) {		    Packet.writeBytesNoNull(streamToBytes(_ParameterStreams[i]));		}		else {		    if (do_escape_processing) {			_ParameterStrings[i] = _Escaper.escapeSQL(_ParameterStrings[i]);		    }					    if (Encoding != null) {			Packet.writeStringNoNull(_ParameterStrings[i], Encoding);		    }		    else {			Packet.writeStringNoNull(_ParameterStrings[i]);		    }		}	    }			    if (Encoding != null) {		Packet.writeStringNoNull(_TemplateStrings[_ParameterStrings.length], Encoding);	    }	    else {		Packet.writeStringNoNull(_TemplateStrings[_ParameterStrings.length]);	    }	}	catch (java.io.UnsupportedEncodingException UE) {	    throw new SQLException("Unsupported character encoding '" + Encoding + "'");	}	ResultSet RS = null;		synchronized (_Conn.getMutex()) {	    String OldCatalog = null;	    if (!_Conn.getCatalog().equals(_Catalog)) {		OldCatalog = _Conn.getCatalog();		_Conn.setCatalog(_Catalog);	    }	    // If there isn't a limit clause in the SQL	    // then limit the number of rows to return in 	    // an efficient manner. Only do this if	    // setMaxRows() hasn't been used on any Statements	    // generated from the current Connection (saves	    // a query, and network traffic).		    if (_Conn.useMaxRows()) {		if (_has_limit_clause) { 		    RS = _Conn.execSQL(null, _max_rows, Packet);		}		else {		    if (_max_rows <= 0) {			_Conn.execSQL("SET OPTION SQL_SELECT_LIMIT=" 				      + MysqlDefs.MAX_ROWS, -1);		    }		    else {			_Conn.execSQL("SET OPTION SQL_SELECT_LIMIT=" + _max_rows,-1);		    }                		    RS = _Conn.execSQL(null, -1, Packet);		}	    }	    else {		RS = _Conn.execSQL(null, -1, Packet);	    	    }	    if (OldCatalog != null) {		_Conn.setCatalog(OldCatalog);	    }	}		    	_last_insert_id = RS.getUpdateID();	if (RS != null) {	    _Results = RS;	}	_escapeProcessing = do_escape_processing;		RS.setConnection(_Conn);	return (RS != null && RS.reallyResult());    }    public String toString()    {	String Encoding = null;	if (_Conn.useUnicode()) {	    Encoding = _Conn.getEncoding();	}	StringBuffer SB = new StringBuffer();	SB.append(super.toString());	SB.append(": ");		try {	    for (int i = 0 ; i < _ParameterStrings.length ; ++i) {	    		if (Encoding != null) {		    SB.append(new String(_TemplateStrings[i].getBytes(), Encoding));		}		else {		    SB.append(_TemplateStrings[i]);		}	    	if (_ParameterStrings[i] == null && 		    (_IsStream[i] && _ParameterStreams[i] == null)) {		    SB.append("** NOT SPECIFIED **");		}		else if (_IsStream[i]) {		    SB.append("** STREAM DATA **");		}		else {		    if (_escapeProcessing) {			try {			    _ParameterStrings[i] = _Escaper.escapeSQL(_ParameterStrings[i]);			}			catch (SQLException SQE) {}		    }				    if (Encoding != null) {			SB.append(new String(_ParameterStrings[i].getBytes(), Encoding));		    }		    else {			SB.append(_ParameterStrings[i]);		    }		}	    }		    if (Encoding != null) {		SB.append(new String(_TemplateStrings[_ParameterStrings.length].getBytes(), Encoding));	    }	    else {		SB.append(_TemplateStrings[_ParameterStrings.length]);	    }	}	catch (java.io.UnsupportedEncodingException UE) {	    SB.append("\n\n** WARNING **\n\n Unsupported character encoding '");	    SB.append(Encoding);	    SB.append("'");	}	return SB.toString();    }		          /**     * There are a lot of setXXX classes which all basically do     * the same thing.  We need a method which actually does the     * set for us.     *     * @param paramIndex the index into the inString     * @param s a string to be stored     * @exception java.sql.SQLException if something goes wrong     */    private final void set(int paramIndex, String S) throws java.sql.SQLException    {	if (paramIndex < 1 || paramIndex > _TemplateStrings.length) {	    throw new java.sql.SQLException("Parameter index out of range (" + paramIndex + " > " + _TemplateStrings.length + ").", "S1009");	}	_ParameterStrings[paramIndex - 1] = S;    }    private final int readblock(InputStream i,byte[] b) throws java.sql.SQLException    {	try  {	    return i.read(b);	}	catch (Throwable E) {            throw new java.sql.SQLException("Error reading from InputStream " +					    E.getClass().getName(), "S1000");	}    }    private final void escapeblock(byte[] buf,ByteArrayOutputStream BytesOut,int				   size)    {	int c =0;    	for (int i=0;i<size;i++) {	    byte b = buf[i];	    if (b == '\0') {		BytesOut.write('\\');		BytesOut.write('0');	    }	    else {		if (b == '\\' || b == '\'' || b == '"') {		    BytesOut.write('\\');		}		BytesOut.write(b);	    }	}    }    /**     * For the setXXXStream() methods. Basically converts an     * InputStream into a String. Not very efficient, but it     * works.     *     */         private final byte[] streamToBytes(InputStream In) throws java.sql.SQLException    {	byte[] bi=new byte[128*1024];	ByteArrayOutputStream BytesOut = new ByteArrayOutputStream();	int bc = readblock(In,bi); 	BytesOut.write('\'');   	while (bc > 0) {	    escapeblock(bi,BytesOut,bc);	    bc = readblock(In,bi);	}   	BytesOut.write('\'');   	return BytesOut.toByteArray();    }    private final char getSuccessor(char c,int n)     {	return  (c=='y' && n==2) ? 'X' : //ym	    ((c=='y' && n<4) ? 'y' :	     ((c=='y') ? 'M' :	      ((c=='M' && n==2) ? 'Y' : //Md	       ((c=='M' && n<3) ? 'M' :		((c=='M') ? 'd' :		 ((c=='d' && n<2) ? 'd' :		  ((c=='d') ? 'h' :		   ((c=='h' && n<2) ? 'h' :		    ((c=='h') ? 'm' :		     ((c=='m' && n<2) ? 'm' :		      ((c=='m') ? 's' :		       ((c=='s' && n<2) ? 's' : 'W' ))))))))))));    }    private final String getDateTimePattern(String dt,boolean toTime) throws Exception     {	int n,z,count,maxvecs;	char c,separator;	StringReader reader=new StringReader(dt+" ");	Vector vec=new Vector();	Vector vec_removelist=new Vector();	Object[] nv=new Object[3];	Object[] v;	nv[0]=new Character('y');	nv[1]=new StringBuffer();	nv[2]=new Integer(0);	vec.addElement(nv);	if (toTime) {	    nv=new Object[3];	    nv[0]=new Character('h');	    nv[1]=new StringBuffer();	    nv[2]=new Integer(0);	    vec.addElement(nv);	}	while ((z=reader.read())!=-1) {	    separator=(char)z;	    maxvecs=vec.size();	    for(count=0;count<maxvecs;count++) {		v=(Object [])vec.elementAt(count);		n=((Integer)v[2]).intValue();		c = getSuccessor(((Character)v[0]).charValue(),n);		if (!Character.isLetterOrDigit(separator)) {		    if ((c==((Character)v[0]).charValue())&&(c!='S'))			vec_removelist.addElement(v);		    else {			((StringBuffer)v[1]).append(separator);			if (c=='X' || c=='Y')			    v[2]=new Integer(4);		    }		} else {		    if (c=='X') {			c='y';			nv=new Object[3];			nv[1]=(new StringBuffer(((StringBuffer)v[1]).toString())).append('M');			nv[0]=new Character('M');			nv[2]=new Integer(1);			vec.addElement(nv);		    } else if (c=='Y') {			c='M';			nv=new Object[3];			nv[1]=(new StringBuffer(((StringBuffer)v[1]).toString())).append('d');			nv[0]=new Character('d');			nv[2]=new Integer(1);			vec.addElement(nv);		    }		    ((StringBuffer)v[1]).append(c);					    if (c==((Character)v[0]).charValue())			v[2]=new Integer(n+1);		    else {			v[0]=new Character(c);			v[2]=new Integer(1);		    }		}	    }	    for(Enumeration en=vec_removelist.elements();		en.hasMoreElements();) {		v=(Object [])en.nextElement();		vec.removeElement(v);	    }	    vec_removelist.removeAllElements();	}	for(Enumeration en=vec.elements();en.hasMoreElements();) {	    v=(Object [])en.nextElement();	    c=((Character)v[0]).charValue();	    n=((Integer)v[2]).intValue();	    boolean bk=getSuccessor(c,n)!=c;	    boolean atEnd=((c=='s'||c=='m'||(c=='h' && toTime))&&bk);	    boolean finishesAtDate=(bk&&(c=='d')&& !toTime);	    boolean containsEnd=(((StringBuffer)v[1]).toString().indexOf('W')!=-1);	    if ((!atEnd && !finishesAtDate) || (containsEnd)) {		vec_removelist.addElement(v);	    }	}	for(Enumeration en=vec_removelist.elements();en.hasMoreElements();)	    vec.removeElement(en.nextElement());	vec_removelist.removeAllElements();	v=(Object [])vec.firstElement(); //might throw exception	StringBuffer format=((StringBuffer)v[1]);	format.setLength(format.length()-1);	return format.toString();    }      /**     * Formatter for double - Steve Ferguson     */      private static NumberFormat _DoubleFormatter;      // Class Initializer	    static {	_DoubleFormatter = 	    NumberFormat.getNumberInstance(java.util.Locale.US);	_DoubleFormatter.setGroupingUsed(false);	// attempt to prevent truncation	_DoubleFormatter.setMaximumFractionDigits(8);     }};

⌨️ 快捷键说明

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