Preach the _3_jdbc of knowledge

Source: Internet
Author: User

JDBC the core of programming API

DriverManager -Getting database connections

Connection -Represents a connection to a database that creates a statement,preparedstatement.

Statement/preparedstatement -Execute SQL.

Managing result Sets

= = = Updatable result set ===demo

The result set we queried earlier was just a read of the data,

But in fact we can also update the result set.

If you are reading resultset content, getxxx (column name | column index)

If you are updating the contents of resultset, updatexxx (column name | column index, newval)

In most cases, we should consider using an update statement to update the data.

In some cases, however, this updatable result set can be used to update the data that is queried.

Rs.updaterow ();

JDBC Programming steps:

1. load driver.

2. Establishes and specifies a connection between databases.

DriverManager. getconnection (URL, user, pass);

3. Create Statement prepare to execute the SQL statement.

4. Execute the SQL statement using statement.

ResultSet ExecuteQuery can only perform a select. It returns the result set of the query,ResultSet

The Int executeupdate only performs DML and DDL. If the DDL is executed, it always returns 0, and if DML is specified, the number of record bars affected is returned.

Boolean execute can execute all of the SQL statements. DEMO

5. Get the results of execution.

6. Turn off physical resources.

Later we use JDBC, generally do not statement, always use PreparedStatement.

Whether it's statement, or PreparedStatement, there are 3 ways they execute SQL statements:

ResultSet executequery(SELECT statement)

int executeupdate(DDL/DML)

Boolean execute(any SQL statement): The return value of the method represents whether a result set has been completed after execution of the SQL statement.

 

 

 

setstring

updatestring

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.

Method Executeupdate
Used to execute INSERT, UPDATE, or DELETE statements, and 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 the executeupdate is an integer indicating the number of rows affected (that is, the update count). For statements that do not manipulate rows such as CREATE table or DROP TABLE, theexecuteupdate return value is always zero.

The Executeupdate method is used because the SQL statement in Createtablecoffees is a DDL (data definition Language) statement. Creating tables, changing tables, and deleting tables are examples of DDL statements that are executed using the Executeupdate method. You can also see from its name that the method executeupdate is also used to execute the Update table SQL statement. In fact, executeupdate is used to update tables more often than creating tables, because tables need to be created once but are frequently updated.

MethodExecute:
Used to execute statements that return multiple result sets, multiple update counts, or combinations of both. Because most programmers don't need this advanced feature

The Execute method should be used only if the statement can return multiple ResultSet objects, multiple update counts, or a combination of the ResultSet object and the update count. When executing a stored procedure or dynamically executing an unknown SQL string (that is, an application programmer is unknown at compile time), it is rare that a number of results may occur.
Because method execute handles unconventional situations, it is not surprising that obtaining the results requires some special treatment. For example, assuming that a procedure is known to return two result sets, after you use method execute to execute the procedure, you must call method Getresultset to get the first result set, and then call the appropriate GetXXX method to get the value in it. To get a second result set, you need to call the Getmoreresults method first, and then call the Getresultset method. If a procedure is known to return two update counts, the method Getupdatecount is called first, then the getmoreresults is invoked, and the getupdatecount is called again.
The situation is more complex for those that do not know what to return. If the result is a ResultSet object, method execute returns True, or False if the result is a Java int. If int is returned, it means that the result is an update count or that the statement being executed is a DDL command. The first thing to do after invoking method execute is to invoke Getresultset or Getupdatecount. Call method Getresultset can get the first object in two or more ResultSet objects, or call method Getupdatecount can get the contents of the first update count in two or more update counts.
The method Getresultset returns NULL when the result of the SQL statement is not a result set. This may mean that the result is an update count or no other result. In this case, the only way to determine the true meaning of NULL is to invoke the method Getupdatecount, which returns an integer. This integer is the number of rows affected by the calling statement, or 1 to indicate that the result is a result set or no result. If the method Getresultset has returned null (indicating that the result is not a ResultSet object), the return value of 1 indicates no other result. This means that there is no result (or no other result) when the following conditions are true:
((stmt.getresultset () = = null) && (stmt.getupdatecount () = = 1))

If you have called the method Getresultset and processed the ResultSet object it returns, it is necessary to call the methodGetmoreresultsTo determine if there are other result sets or update counts. IfGetmoreresultsReturns true, you need to call Getresultset again to retrieve the next result set. As mentioned, if Getresultset returns NULL, you need to call theGetupdatecountTo check for NULL to indicate whether the result is an update count or that there are no other results.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.