KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > sql > PreparedStatement

java.sql
Interface PreparedStatement

All Superinterfaces:
Statement
All Known Subinterfaces:
CallableStatement
See Also:
Top Examples, Source Code, Connection.prepareStatement(java.lang.String), ResultSet

void addBatch()
              throws SQLException
See Also:
Statement.addBatch(java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[636]Batch exceute SQL statements
By Anonymous on 2005/07/19 11:52:59  Rate
Connection con = DriverManager.getConnection ( connect ) ; 
 Statement stmt = con.createStatement (  ) ; 
 stmt.addBatch ( "INSERT INTO Names VALUES ( 'Jon' ) " ) ; 
 stmt.addBatch ( "INSERT INTO Names VALUES ( 'Joe' ) " ) ; 
 stmt.addBatch ( "INSERT INTO Names VALUES ( 'Jim' ) " ) ; 
 int  [  ]  updateCounts = stmt.executeBatch (  ) ;


void clearParameters()
                     throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


boolean execute()
                throws SQLException
See Also:
Statement.getMoreResults(), Statement.getUpdateCount(), Statement.getResultSet(), Statement.execute(java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


ResultSet executeQuery()
                       throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[40]Prepare and execute a SQL query
By Anonymous on 2002/09/09 20:00:17  Rate
Connection con = null; 
 PreparedStatement ps = null; 
  
  
 try  {  
   con = getConnection (  ) ; 
   ps = con.prepareStatement ( "select aField from aTable where aID = ?" ) ; 
   ps.setInt ( 1, 99 ) ; 
   ps.executeQuery (  ) ; 
   ResultSet rs = ps.getResultSet (  ) ; 
   if  ( rs.next (  )  )   {  
   String aField = rs.getString ( 1 ) ; 
  }  else  {  
   System.out.println ( "record is not found." ) ; 
  }  
  }  catch  ( SQLException sqe )   {  
   System.out.println ( "SQLException: " + sqe ) ; 
  }  finally  {  
   try  {  
     if  ( ps != null )  ps.close (  ) ; 
    }  catch  ( Exception e )   {  
     System.out.println ( "Error closing PreparedStatement: "+e ) ; 
    }  
  
  
   try  {  
     if  ( con != null )  con.close (  ) ; 
    }  catch  ( Exception e )   {  
     System.out.println ( "Error closing Connection: " + e ) ; 
    }  
  } 


[1899]Reading BLOBs from database
By Anonymous on 2007/06/30 09:51:04  Rate
pssel.setString ( 1, name ) ;    // Set search value of PreparedStatement 
 rs = pssel.executeQuery (  ) ;    // Execute PreparedStatement 
 while  ( rs.next (  )  )   {     // Loop through records 
 bs = rs.getBinaryStream ( 3 ) ;  // Open a InputStream to the BLOB 
 bytes = bs.read ( buf ) ;    // Read bytes from BLOB stream


int executeUpdate()
                  throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[398]SQL update
By Anonymous on 2005/02/10 08:22:46  Rate
public void updateSalary ( String empName, int empSalary )   {  
         Connection con = null; 
         PreparedStatement psUpdateSalary = null; 
         String QueryUpdateSal = "update Employee set EMP_SALARY = ? where EMP_NAME = ?"; 
         ResultSet rs = null; 
  
  
         con = getConnection (  ) ; 
  
  
         try  {  
             psUpdateSalary = con.prepareStatement ( QueryUpdateSal ) ; 
  
  
             psUpdateSalary.setInt ( 1, empSalary ) ; 
             psUpdateSalary.setString ( 2, empName ) ; 
  
  
             psUpdateSalary.executeUpdate (  ) ; 
             psUpdateSalary.close (  ) ; 
             con.close (  ) ; 
          }  
         catch  ( SQLException sqe )   {  
             System.out.println ( "SQL Exception Occured " + sqe ) ; 
          }  
      }  
 


ResultSetMetaData getMetaData()
                              throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


ParameterMetaData getParameterMetaData()
                                       throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[828]Check out SQL statement parameter MetaData
By Anonymous on 2004/07/05 02:57:15  Rate
Connection con; 
 ParameterMetaData pmtadta; 
 int mtadtacnt; 
 int sqlType; 
 ... 
 pstmt = con.prepareStatement (  
   "UPDATE EMPLOYEE SET PHONENO=? WHERE EMPNO=?" ) ;  
                                   // Create a PreparedStatement object 1 
 pmtadta = pstmt.getParameterMetaData (  ) ; 2 
                                   // Create a ParameterMetaData object 
 mtadtaint = pmtadta.getParameterCount (  ) ; 3 
                                   // Determine the number of parameters 
 System.out.println ( "Number of statement parameters: " + mtadtacnt ) ; 
 for  ( int i = 1; i  < = mtadtacnt; i++ )   {  
   sqlType = pmtadta.getParameterType ( i ) ; 4 
                                   // Get SQL type for each parameter 
  System.out.println ( "SQL type of parameter " + i " is " + sqlType ) ; 
  }  
 ... 
 pstmt.close (  ) ; // Close the PreparedStatement                  
  
  
 


void setArray(int i,
              Array x)
              throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1162]setArray does not appear working
By kickjavaspam { at } phora { dot } net on 2005/07/14 13:16:02  Rate
This does not appear to be implemented in JDBC. I would expect it to be for variable-length particles, such as "SELECT x FROM y WHERE z IN  ( ? ) "

void setAsciiStream(int parameterIndex,
                    InputStream x,
                    int length)
                    throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setBigDecimal(int parameterIndex,
                   BigDecimal x)
                   throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setBinaryStream(int parameterIndex,
                     InputStream x,
                     int length)
                     throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[318]Save a really long string to DB
By Anonymous on 2003/07/15 09:05:34  Rate
String longString = "really long ... "; 
 int len = longString.length (  ) ; 
 ByteArrayInputStream stream =  
   new ByteArrayInputStream (  longString.getBytes (  )   ) ; 
 pstmt.setBinaryStream (  1, stream, len  ) ; 
 


[1898]Storing a file in a database BLOB
By Anonymous on 2007/06/30 09:48:46  Rate
File file = new File ( path ) ;    // Create File object for the file 
 long filesize = file.length (  ) ;    // Get size of file 
 fs = new FileInputStream ( file ) ;    // Open the file 
 bs = new BufferedInputStream ( fs, 16384 ) ;  // Use file buffering 
 psins.setBinaryStream ( 2, bs,  ( int ) filesize ) ;


void setBlob(int i,
             Blob x)
             throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setBoolean(int parameterIndex,
                boolean x)
                throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setByte(int parameterIndex,
             byte x)
             throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setBytes(int parameterIndex,
              byte[] x)
              throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[673]Save Blob
By Anonymous on 2004/02/24 09:53:05  Rate
private void saveBlob ( String filename,byte [  ]  out )  {  
      
 Connection con = null; 
 PreparedStatement pstmt = null; 
 String sqlCmd = 
   "INSERT INTO BLOBS  ( FileName,BinaryData )  VALUES ( ?,? ) "; 
 try  {  
   Class.forName ( jdbcDriver ) ; 
   con = DriverManager.getConnection ( dbUrl ) ; 
  
  
   pstmt = con.prepareStatement ( sqlCmd ) ; 
   pstmt.setString ( 1, filename ) ; 
   pstmt.setBytes ( 2, out ) ; 
   pstmt.executeUpdate (  ) ; 
  
  
   con.close (  ) ; 
  }  
 catch ( ClassNotFoundException e )  {  
   e.printStackTrace (  ) ; 
  }  
 catch ( SQLException e )  {  
   e.printStackTrace (  ) ; 
  }  
  
  
  } 


void setCharacterStream(int parameterIndex,
                        Reader reader,
                        int length)
                        throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setClob(int i,
             Clob x)
             throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setDate(int parameterIndex,
             Date x)
             throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[416]Set to today date
By Anonymous on 2003/09/25 13:50:32  Rate
//Set to today's date 
 pstmt.setDate ( 3, new java.sql.Date ( System.currentTimeMillis (  )  )  ) ;


void setDate(int parameterIndex,
             Date x,
             Calendar cal)
             throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[499]Set to a specific date
By haze219 { at } yahoo { dot } com on 2004/12/29 18:31:09  Rate
Calendar OrdDate = Calendar.getInstance (  ) ; 
 SimpleDateFormat formatter = new SimpleDateFormat ( "E dd.MM.yyyy" ) ; 
 public Date OrderDate = new Date (  ) ; 
  
  
 po.OrdDate.set ( Calendar.DAY_OF_MONTH, 10 ) ; 
 po.OrdDate.set ( Calendar.MONTH, 5 ) ; 
 po.OrdDate.set ( Calendar.YEAR, 2003 ) ; 
      
  
  
  
 pstmt.setDate ( 9, OrderDate, OrdDate ) ;


void setDouble(int parameterIndex,
               double x)
               throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setFloat(int parameterIndex,
              float x)
              throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setInt(int parameterIndex,
            int x)
            throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[213]Set integer
By Anonymous on 2003/04/01 11:59:14  Rate
if  ( objs [ i ]  instanceof Integer )   {  
               stmt.setInt ( i+1,  (  ( Integer ) objs [ i ]  ) .intValue (  )  ) ; 
            } 


void setLong(int parameterIndex,
             long x)
             throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setNull(int parameterIndex,
             int sqlType)
             throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[296]Set null
By Anonymous on 2003/07/08 17:19:32  Rate
ps.setNull ( index, Types.INTEGER ) ;

[297]_
By Anonymous on 2003/07/08 17:21:05  Rate
//ps is a PreparedStatement  
 ps.setNull ( index, Types.VARCHAR ) ; 
 ps.setNull ( index, Types.DATE ) ; 
 ps.setNull ( index, Types.INTEGER ) ;


void setNull(int paramIndex,
             int sqlType,
             String typeName)
             throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setObject(int parameterIndex,
               Object x)
               throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setObject(int parameterIndex,
               Object x,
               int targetSqlType)
               throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setObject(int parameterIndex,
               Object x,
               int targetSqlType,
               int scale)
               throws SQLException
See Also:
Types
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setRef(int i,
            Ref x)
            throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setShort(int parameterIndex,
              short x)
              throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setString(int parameterIndex,
               String x)
               throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setTime(int parameterIndex,
             Time x)
             throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setTime(int parameterIndex,
             Time x,
             Calendar cal)
             throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setTimestamp(int parameterIndex,
                  Timestamp x)
                  throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setTimestamp(int parameterIndex,
                  Timestamp x,
                  Calendar cal)
                  throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


@Deprecated
void setUnicodeStream(int parameterIndex,
                                 InputStream x,
                                 int length)
                      throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void setURL(int parameterIndex,
            URL x)
            throws SQLException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags