The differences and usages between execute, ExecuteQuery, and executeupdate provided by the PreparedStatement interface in JDBC(2012-08-27 09:36:18)
reproduced
Tags: statement execute executequery executeupdate |
Category: Database area |
The PreparedStatement interface provides three ways to execute SQL statements: ExecuteQuery, Executeupdate, and execute. Which method to use is determined by the content produced by the SQL statement.
1. Method ExecuteQuery
The statement used to produce a single result set, such as a SELECT statement. The most used method of executing SQL statements is executeQuery. This method is used to execute the SELECT statement, which is almost the most frequently used SQL statement.
2. Method Executeupdate
Used to execute INSERT, UPDATE, or DELETE statements, as well as SQL DDL (data definition language) statements, such as CREATE table and DROP table. The effect of an INSERT, UPDATE, or DELETE statement is to modify one or more columns in 0 or more rows in a table. The return value of Executeupdate is an integer that indicates the number of rows affected (that is, the update count). For statements that do not manipulate rows such as CREATE table or DROP table, the return value of executeupdate is always zero.
The Executeupdate method is used because the SQL statement in Createtablecoffees is a DDL (data definition Language) statement. Creating tables, altering tables, and deleting tables are examples of DDL statements that are executed using the Executeupdate method. You can also see from its name that method Executeupdate is also used to execute an UPDATE table SQL statement. In fact, Executeupdate is more time-dependent for updating tables than creating tables, because tables need to be created only once, but are often updated.
3. Method Execute:
Used to perform statements that return multiple result sets, multiple update counts, or a combination of both. It can also be used to execute an INSERT, UPDATE, or DELETE statement.
Examples of usage:
1, add, modify, delete all with Execute (), also can be used executeupdate (), for INSERT, UPDATE or DELETE statement
public int addairenvironmentpresent (M_airenviromentpresentdto airdto) {
int index = 1;
String sql = "INSERT into airpresent (Airforecastplace,forecasttime,tspvalue,remark) VALUES (?,?,?,?)";
try {
PS = conn.preparestatement (SQL);
Ps.setstring (index++, Airdto.getairforecastplace ());
Ps.setstring (index++, Airdto.getforecasttime ());
Ps.setstring (index++, Airdto.gettspvalue ());
Ps.setstring (index++, Airdto.getremark ());
Ps.execute ();
} catch (SQLException e) {
E.printstacktrace ();
}
return 1;
}
2, Query call ExecuteQuery (), for the SELECT statement
Public ArrayList Getairenvironmentpresentall () {
arraylist list = new ArrayList ();
string sql = "SELECT * from Airpresent";
try {
ps = conn.preparestatement (sql);
rs = Ps.executequery ();
while (Rs.next ()) {
dto = new M_airenviromentpresentdto ();
dto.setid (Rs.getint ("id"));
dto.setairforecastplace (rs.getstring ("Airforecastplace"));
dto.setforecasttime (rs.getstring ("Forecasttime"));
dto.settspvalue (rs.getstring ("Tspvalue"));
dto.setremark (rs.getstring ("remark"));
list.add (DTO);
}
} catch (SQLException e) {
e.printstacktrace ();
}
return list;
}
The differences and usages between execute, ExecuteQuery, and executeupdate provided by the PreparedStatement interface in JDBC