16-1-27---JDBC review (01)

Source: Internet
Author: User

JDBC Database Connection Learning
Connect to the database using JDBC
1. Load Driver
Class.forName ("");
Registering means that there are two objects in memory, and using the method above only one driver in memory does not need to rely on a specific API program to be more flexible
2. Get Connected
Connection conn = DriverManager ();
3. Creating SQL Objects
PreparedStatement
CallableStatement
Statement Statement = Conn.createstatement ();
4. Sending SQL instructions to the database via statement
Statement.executeupdate (SQL); Execute DML (update insert Delete)
Statement.executequery (SQL); Perform
5. Close the resource (first open, then closed, then open first closed)

Connection he represents the connection to the database. All client-to-database interactions are done through the connection object.
Createstatement (); Create a statement object that sends SQL to the database
Preparestatement (SQL); Creates a Perparestatement object that sends precompiled SQL to the database. can prevent SQL injection
Prepare (SQL); Create an CallableStatement object in the execution stored procedure

When do you need it? Setautocommit set to False,setautocommit (Boolean autocommit); Set whether the transaction is automatically committed, commit a set of DML statements together, use transaction management, set to False (equivalent to atomic, I understand);

Commit ();
Rollback ();
The ResultSet object calls the next () method to return the last false; note that false is not NULL
Statement stmt = con.createstatement (resultset.type_scroll_insensitive, resultset.concur_updatable);
Static constants in the ResultSet
Close_cursors_at_commit The constant indicates that an open ResultSet object with this persistence will be closed when the current transaction is committed.
Concur_read_only This constant indicates that the concurrency mode of the ResultSet object cannot be updated.
Concur_updatable This constant indicates the concurrency pattern of the ResultSet object that can be updated.
Fetch_forward The constant indicates that the rows in the result set will be processed in a positive direction (that is, from the first to the last).
Fetch_reverse
Fetch_unknown
Hold_cursors_over_commit
Type_forward_only
Type_scroll_insensitive This constant indicates the type of ResultSet object that can be scrolled but is generally not affected by ResultSet underlying data changes.
Type_scroll_sensitive This constant indicates the type of ResultSet object that can be scrolled and is typically affected by ResultSet underlying data changes.
ResultSet rs = stmt.executequery ("Select a, b from table");

ResultSet By default, our RS result set can only move forward so that the RS result set cannot be reused.
We can do this.
statement = Ct.createstatement (resultset.type_scroll_insensitive, resultset.concur_read_only);

Rs.beforefirst (); We want to reuse the result set,
Rs.next (); Move to the next line
Rs.        Previous (); Move to Previous line
Rs.absolute (row); Move to the specified line
Rs.afterlast (); Move to the last side of the resultset

!! Question if the type of column in the database is VARCHAR2, what is the data call for that column? What about the int type? What about the bigint type? What about the Boolean type?
MySQL database jdbc corresponding type data table

MySQL connection concurrent, maximum connection 13000

Close resources instantly, and a resource does not shut down will also cause problems.
Connection connect as late as possible, release as early as possible, and if you cannot handle the connection, there is a high likelihood of downtime.
The code to close the resource must be put in the finally

Writing of the SqlHelper class
1, access to the database is very ordinary, there is concurrency, do not set the connection to static
2, load the driver will need one time

16-1-27---JDBC review (01)

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.