?? insertps.html
字號:
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>Inserting a Row into a Database Table Using a Prepared Statement
(Java Developers Almanac Example)
</TITLE>
<META CONTENT="Patrick Chan" NAME="AUTHOR">
<META CONTENT="Code Examples from The Java Developers Almanac 1.4" NAME="DESCRIPTION">
<META CONTENT="Addison-Wesley/Patrick Chan" NAME="OWNER">
<META CONTENT="3/20/02" NAME="revision">
<STYLE TYPE="text/css">
<!-- BODY CODE {font-family: Courier, Monospace; font-size: 11pt} TABLE, BODY {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt} PRE {font-family: Courier, Monospace; font-size: 10pt} H3 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11pt} A.eglink {text-decoration: none} A:hover.eglink {text-decoration: underline} -->
</STYLE>
</HEAD>
<BODY>
<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">
<TR>
<TD rowspan="3"><A HREF="/?l=ex"><IMG BORDER="0" ALIGN="BOTTOM" HSPACE="10" SRC="/egs/almanac14a.jpg"></A></TD><TD VALIGN="top"><font face="Times" size="6"><b>The Java Developers Almanac 1.4</b></font>
<br>
Order this book from <a href="/cgi-bin/scripts/redirect.pl?l=ex&url=http://www.amazon.com/exec/obidos/ASIN/0201752808/xeo">Amazon</a>.
</TD>
</TR>
<TR>
<TD align="right" valign="bottom">
<FORM method="get" action="/cgi-bin/search/find.pl">
<INPUT size="25" name="words" type="text"><INPUT value="Search" type="submit">
</FORM>
</TD>
</TR>
</TABLE>
<HR color="#6666cc">
<DIV ALIGN="LEFT">
<A HREF="/">Home</A>
>
<A HREF="../index.html">List of Packages</A>
>
<B><A HREF="../java.sql/pkg.html">java.sql</A></B><font color="#666666" SIZE="-2">
[73 examples]
</font>
>
<B><A HREF="../java.sql/pkg.html#Inserting%20and%20Updating%20Data">Inserting and Updating Data</A></B><font color="#666666" SIZE="-2">
[4 examples]
</font>
</DIV><P>
<h3>
e259.
Inserting a Row into a Database Table Using a Prepared Statement</h3>
If you have a SQL statement that needs to be executed many times but
with different values, a prepared statement can be used to improve
performance. For example, if you have a website that looks up product
information with a product id using the same query each time, a
prepared statement should be used. A prepared statement is a
precompiled SQL statement and its use saves the database from
repeatedly having to compile the SQL statement each time it is
executed.
<P> A query in a prepared statement contains placeholders
(represented by the '?' character) instead of explicit values. You
set values for these placeholders and then execute the prepared
statement.
<pre>
try {
// Prepare a statement to insert a record
String sql = <font color="#0066ff"><i>"INSERT INTO my_table (col_string) VALUES(?)"</i></font>;
PreparedStatement pstmt = <font color="#0066ff"><i>connection</i></font>.prepareStatement(sql);
// Insert 10 rows
for (int i=0; i<<font color="#0066ff"><i>10</i></font>; i++) {
// Set the value
pstmt.setString(1, <font color="#0066ff"><i>"row "+i</i></font>);
// Insert the row
pstmt.executeUpdate();
}
} catch (SQLException e) {
}
</pre>
Here is another example of inserting with a prepared statement that
uses the various <code>setXXX()</code> methods. This example uses the table
created in <a href="../java.sql/CreateCompleteMySqlTable.html" class="eglink"><font size="-1"><b>e248</b> Creating a MySQL Table to Store Java Types</font></a>.
<pre>
try {
// Prepare a statement to insert a record
String sql = "INSERT INTO mysql_all_table("
+ "col_boolean,"
+ "col_byte,"
+ "col_short,"
+ "col_int,"
+ "col_long,"
+ "col_float,"
+ "col_double,"
+ "col_bigdecimal,"
+ "col_string,"
+ "col_date,"
+ "col_time,"
+ "col_timestamp,"
+ "col_asciistream,"
+ "col_binarystream,"
+ "col_blob) "
+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement pstmt = <font color="#0066ff"><i>connection</i></font>.prepareStatement(sql);
// Set the values
pstmt.setBoolean(1, <font color="#0066ff"><i>true</i></font>);
pstmt.setByte(2, <font color="#0066ff"><i>(byte)123</i></font>);
pstmt.setShort(3, <font color="#0066ff"><i>(short)123</i></font>);
pstmt.setInt(4, <font color="#0066ff"><i>123</i></font>);
pstmt.setLong(5, <font color="#0066ff"><i>123L</i></font>);
pstmt.setFloat(6, <font color="#0066ff"><i>1.23F</i></font>);
pstmt.setDouble(7, <font color="#0066ff"><i>1.23D</i></font>);
pstmt.setBigDecimal(8, new BigDecimal(<font color="#0066ff"><i>1.23</i></font>));
pstmt.setString(9, <font color="#0066ff"><i>"a string"</i></font>);
pstmt.setDate(10, <font color="#0066ff"><i>new java.sql.Date(System.currentTimeMillis())</i></font>);
pstmt.setTime(11, <font color="#0066ff"><i>new Time(System.currentTimeMillis())</i></font>);
pstmt.setTimestamp(12, <font color="#0066ff"><i>new Timestamp(System.currentTimeMillis())</i></font>);
// Set the ascii stream
File file = new File(<font color="#0066ff"><i>"infilename1"</i></font>);
FileInputStream is = new FileInputStream(file);
pstmt.setAsciiStream(13, is, (int)file.length());
// Set the binary stream
file = new File(<font color="#0066ff"><i>"infilename2"</i></font>);
is = new FileInputStream(file);
pstmt.setBinaryStream(14, is, (int)file.length());
// Set the blob
file = new File(<font color="#0066ff"><i>"infilename3"</i></font>);
is = new FileInputStream(file);
pstmt.setBinaryStream(15, is, (int)file.length());
// Insert the row
pstmt.executeUpdate();
} catch (SQLException e) {
} catch (FileNotFoundException e) {
}
</pre>
<P><table width="600" CELLSPACING="0" CELLPADDING="2" BORDER="0">
<tr>
<td bgcolor="#6666cc" align="center"><font color="#ffffff">
Related Examples
</font></td>
</tr>
</table>
e258. <a class="eglink" href="Insert.html?l=rel">
Inserting a Row into a Database Table
</a>
<br>
e260. <a class="eglink" href="InsertBinary.html?l=rel">
Getting and Inserting Binary Data into an Database Table
</a>
<br>
e261. <a class="eglink" href="UpdateData.html?l=rel">
Updating a Row in a Database Table
</a>
<br>
<table width="600" CELLSPACING="0" CELLPADDING="2" BORDER="0">
<tr>
<td align="left">
<br>
See also:
<a class="eglink" href="/egs/java.sql/pkg.html?l=rel#Batching">
Batching
</a>
<a class="eglink" href="/egs/java.sql/pkg.html?l=rel#Connections">
Connections
</a>
<a class="eglink" href="/egs/java.sql/pkg.html?l=rel#Database%20Meta%20Data">
Database Meta Data
</a>
<a class="eglink" href="/egs/java.sql/pkg.html?l=rel#Deleting%20Data">
Deleting Data
</a>
<a class="eglink" href="/egs/java.sql/pkg.html?l=rel#Drivers">
Drivers
</a>
<a class="eglink" href="/egs/java.sql/pkg.html?l=rel#Importing%20and%20Exporting">
Importing and Exporting
</a>
<a class="eglink" href="/egs/java.sql/pkg.html?l=rel#Oracle%20OBJECTs">
Oracle OBJECTs
</a>
<a class="eglink" href="/egs/java.sql/pkg.html?l=rel#Oracle%20VARRAYs">
Oracle VARRAYs
</a>
<a class="eglink" href="/egs/java.sql/pkg.html?l=rel#Procedures%20and%20Functions">
Procedures and Functions
</a>
<a class="eglink" href="/egs/java.sql/pkg.html?l=rel#Retrieving%20Data">
Retrieving Data
</a>
<a class="eglink" href="/egs/java.sql/pkg.html?l=rel#Scrollable%20Result%20Sets">
Scrollable Result Sets
</a>
<a class="eglink" href="/egs/java.sql/pkg.html?l=rel#Tables">
Tables
</a>
<a class="eglink" href="/egs/java.sql/pkg.html?l=rel#Updatable%20Result%20Sets">
Updatable Result Sets
</a>
</td>
</tr>
</table>
<br>
<br>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif" SIZE="0">
© 2002 Addison-Wesley.
</FONT>
</BODY>
</HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -