The Statement 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
A statement used to produce a single result set (ResultSet), such as a SELECT statement. The most used method of executing the SQL statement. This method is used to execute the SELECT statement, which is almost the most frequently used SQL statement. However, only query statements can be executed, and ResultSet objects that represent the results of the query are returned after execution.
Such as:
Load Database Driver
Class.forName ("com. MySQL.jdbc.Driver ");
Get a database connection using DriverManager
Connection conn = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test",
"Root", "1234");
Use connection to create a Statment object
Statement stmt = Conn.createstatement ();
Execute Query statement
ResultSet rs =stmt.executequery ("Select * from teacher");
Output the query results to
while (Rs.next ())
{
System.out.println (Rs.getint (1) + "/t" + rs.getstring (2));
}
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 (int) 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.
Such as:
Load Database Driver
Class.forName ("Com.mysql.jdbc.Driver");
Get a database connection using DriverManager
Connection conn = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test",
"Root", "1234");
Use connection to create a Statment object
Statement stmt = Conn.createstatement ();
Executes a DML statement that returns the number of affected record bars
return stmt.executeupdate (SQL);
3> Method Execute:
Can be used to execute any SQL statement, returning a Boolean value indicating whether the SQL statement executed returned a resultset. Returns true if the first result after execution is resultset, otherwise false is returned. However, it is cumbersome to execute SQL statements, and it is often not necessary to use the Execute method to execute SQL statements, but rather to use executequery or executeupdate more suitable, However, if you do not know the type of the SQL statement, you can only use the Execute method to execute the SQL statement.
such as://Load Drive
Class.forName (driver);
Get database connection
conn = drivermanager.getconnection (URL, user, pass);
Use connection to create a Statment object
stmt = Conn.createstatement ();
Executes SQL that returns a Boolean value indicating whether the resultset is included
Boolean hasresultset = Stmt.execute (sql);
If there is a resultset result set after execution
if (Hasresultset)
{
Get result set
rs = Stmt.getresultset ();
ResultSetMetaData is a meta-data interface for analyzing result sets
ResultSetMetaData RSMD = Rs.getmetadata ();
int columnCount = Rsmd.getcolumncount ();
Iterate output ResultSet Object
while (Rs.next ())
{//Output values for each column sequentially
for (int i = 0; i < ColumnCount; i++)
{
System.out.print (rs.getstring (i + 1) + "/t");
}
System.out.print ("/n");
}
}
Else
{
System.out.println ("The record affected by this SQL statement has" + stmt.getupdatecount () + "bar");
}
The difference between execute, ExecuteQuery, and executeupdate in JDBC