Java database connection procedure

Source: Internet
Author: User

Java database connection steps

1. Open the connection
 
2. Create a statement set: a normal statement set and a predefined statement set
 
Statement stat = conn. createStatement ();

PreparedStatement priceUpdateStmt =

Conn. prepareStatement ("SQL statement with question mark ");
 
PriceUpdateStmt. setString (1, in. nextLine ());

3. Execute the statement
 
Execteupdate () is used for DELETE, UPDATE, and INSERT operations ()
 
ExecuteUpdate returns an integer number, indicating the number of rows affected during deletion, update, and insertion.
 
ExecuteQuery () when selecting ()
 
The ResultSet is generated only after the executeQuery () method.
 
ResultSet indicates the content of several rows returned after Select.
 
The ResultSet calls the next method to return a row.
 
While (rs. next ())
 
{
 
Result. append (rs. getString (1 ));
 
Result. append (",");
 
Result. append (rs. getString (2 ));
 
Result. append ("\ n ");
 
}

NAME
AGE
 
AAA
33
 
BBB
55 rs

4. Disable the statement set
 
5. Disable the result set.
 
6. Close the connection

Metadata
 
ResultSet result = stat. getResultSet ();
 
ResultSetMetaData metaData = result. getMetaData ();
 
Int columnCount = metaData. getColumnCount ();
 
System. out. print (metaData. getColumnLabel (1 ));

This article permanently updates the link address:

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.