JDBC Basic Learning (i)-jdbc and additions and deletions

Source: Internet
Author: User

First, the persistence of data

Persistence (Persistence): Saves data to an off-the-shelf storage device for later use. In most cases, data persistence means that the data in memory is saved to the hard disk to be cured, and the implementation of the persistence process is mostly done through a variety of relational databases .

The primary application of persistence is to store in-memory data in a relational database and, of course, in disk files and XML data files.

Ii. Introduction to JDBC 1.JDBC overview

JDBC (Java database Connectivity) is a public interface that is independent of a specific database management system, common SQL database access and operations. Defines the standard Java class library used to access the database, which provides easy access to database resources in a standard way.

JDBC provides a unified way to access different databases, shielding developers from some of the details.

2.JDBC Architecture

The JDBC Interface (API) consists of two levels:
application-oriented APIs: Java API, an abstraction interface for application developers to use (connect to a database, execute SQL statements, and get results).
database-oriented APIs: Java Driver API for developers to develop database drivers.

Third, the JDBC operation of the 1.JDBC connection

(1) Registration driver (only once).

(2) Establish the connection (Connection).

(3) Create a statement that executes SQL (Statement).

(4) Execute the statement.

(5) Processing execution results (RESULTSET).

(6) Releasing resources.

public static void Test () throws SQLEXCEPTION{//1. Registered driver Drivermanager.registerdriver (new Com.mysql.jdbc.Driver ()); String url = "Jdbc:mysql://localhost:3306/jdbc"; String user = "root"; String password = "123456";//2. Establish connection Connection con = drivermanager.getconnection (Url,user,password);//3. Create STATEMENT statement st = Con.createstatement ();//4. Execute statement ResultSet rs = st.executequery ("SELECT * from user"); 5. Processing results while (Rs.next ()) {System.out.println (Rs.getobject (1) + "\ T" + rs.getobject (2) + "\ T" + rs.getobject (3) + "\ T" + Rs. GetObject (4));} 6. Release of resources Rs.close (); St.close (); Con.close ();}

Results:

1 Three 2016-04-18 100.0
2 John Doe 2016-04-17 200.0
3 Dynasty 2016-04-16 150.0
4 Mahan 2016-04-14 300.0
5 Dragons 2016-04-01 400.0
6 Zhaohu 2016-04-12 250.0

(1) Registration drive

Class.forName ("Com.mysql.jdbc.Driver");
It is recommended that this approach does not rely on specific driver classes.
Drivermanager.registerdriver (Com.mysql.jdbc.Driver);
Will cause two of the same drivers in the DriverManager, and will be dependent on the specific driver class.
System.setproperty ("Jdbc.drivers", "Driver1:driver2");
Although there is no dependency on the specific driver classes, registration is not easy, so it is seldom used.

(2) Establishing a connection

Connection con = drivermanager.getconnection (url, user, password);

URL format: JDBC: Sub-Protocol: Sub-name//hostname: Port/database name? Property name = attribute value &. User,password can tell the database with the property name = attribute value, and other parameters such as USEUNICODE=TRUE&CHARACTERENCODING=GBK.

For Oracle database connections: Jdbc:oracle:thin: @localhost: 1521:sid
For SQL Server database connection: jdbc:microsoft:sqlserver//localhost:1433; Databasename=sid
For MySQL database connection: jdbc:mysql://localhost:3306/sid

(3) Create a statement

The object is created by calling the Createstatement method of the Connection object.

The object is used to execute a static SQL statement and returns the execution result.

(4) EXECUTE statement

The object is created by calling the Excutequery () method of the Statement object.

The ResultSet object encapsulates the result set of the database operation in the form of a logical table, and the ResultSet interface is implemented by the database vendor

The ResultSet object maintains a cursor to the current data row, and initially, the cursor can be moved to the next line by the next () method of the ResultSet object before the first row.

(5) Releasing resources

Release resultset, Statement,connection.

The database connection (Connection) is a very rare resource that must be released immediately after use, and will cause the system to go down if the Connection is not properly shut down in time. Connection's use principle is to create as late as possible, releasing as early as possible.

(6) Optimized code

Jdbcutils.java

Public final class Jdbcutils{private static string url = "Jdbc:mysql://localhost:3306/jdbc";p rivate static string user = " Root ";p rivate static String password =" 123456 ";p rivate jdbcutils () {}static{try{//Register drive Class.forName (" Com.mysql.jdbc.Driver ");} catch (ClassNotFoundException e) {throw new Exceptionininitializererror (e);}} /** * Get connection * @return * @throws SQLException */public static Connection getconnection () throws Sqlexception{return Driverma Nager.getconnection (Url,user,password);}  /** * Free resources * @param con * @param st * @param rs */public static void Releaseresource (Connection con,statement st,resultset RS) {try{if (rs! = null) {Rs.close ();}} catch (SQLException e) {e.printstacktrace ();} Finally{try{if (st! = null) {Try{st.close ();} catch (SQLException e) {e.printstacktrace ();}}} Finally{if (con! = null) {Try{con.close ();} catch (SQLException e) {e.printstacktrace ();}}}}}

Base.java

public class Base{public static void Main (string[] args) throws Exception{init (); public static void Init () throws exception{connection con = null; Statement st = null; ResultSet rs = null;try{//establish connection con = jdbcutils.getconnection ();//Create Statement st = Con.createstatement ();//EXECUTE Statement rs = St.executequery ("SELECT * from user");//processing result while (Rs.next ()) {System.out.println (Rs.getobject (1) + "\ T" + rs.getobject (2) + "\ T" + rs.getobject (3) + "\ T" + rs.getobject (4));} Finally{jdbcutils.releaseresource (Con,st,rs);}}}

JDBC Basic Learning (i)-jdbc and additions and deletions

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.