In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
JDBC, the full name of Java DataBase Connectivity / java database connection, is the most basic driver tool for using java to connect and operate the database.
Developed by the javasoft department of sun
The earliest JDBC 1.0 was released with JDK1.1
Consists of a set of classes and interfaces written in the Java language
Java API used to execute SQL statements
Can provide unified access to a variety of relational databases
The biggest advantage of using JDBC drivers is that you can access different databases, because different databases themselves provide different drivers to cater to JDBC drivers.
Process flow
Import jar package: drive jar package!
Load driver class: Class.forname (class name)
Give url, username, password
Use the DriverManager class to get the Connection object
JDBC usage process
Import jar package: drive jar package!
Load driver class: Class.forname (class name)
Give url, username, password
Use the DriverManager class to get the Connection object
Four major parameters:
DriverClassName--com.mysql.jdbc.Driver (MySql database)
Url--jdbc:mysql://localhost:3306/ database name (MySql database)
Uesrname
Password
First, load the driver class (register driver)
Class.forName ("com.mysql.jdbc.Driver")
Reflection mechanism to create driver class objects
ForName static method: returns the class object associated with the class or interface of the given string name
Com.mysql.jdbc.Driver
All java.sql.driver implementation classes provide static code blocks, in which the code registers itself with the DriverManage
(register the given driver with DriverManager: the newly loaded driver class should call the registerDriver method to let DriverManager know about itself)
Public class Driver extends NonRegisteringDriver implements java.sql.Driver {
Static {
Try {
Java.sql.DriverManager.registerDriver (new Driver ())
} catch (SQLException E) {
Throw new RuntimeException ("Can't register driver!")
}
}
Public Driver () throws SQLException {
/ / Required for Class.forName () .newInstance ()
}
}
2. Get the connection (via DBUrl, UserName, PassWord)
Connection conn = DriverManager.getConnection (DB_URL, USER, PASS)
Java.sql.DriverManager
Basic services for managing a set of JDBC drivers
Note: the new DataSource interface in JDBC 2.0 API provides another way to connect to a data source. Using the DataSource object is the preferred way to connect to a data source.
Java.sql.Connection
A connection (session) to a specific database. Executes the SQL statement in the context of the connection and returns the result.
Third, operate the database (CURD)
Get Statement
Statement stmt = con.createStatement ()
Java.sql .Statement
The object used to execute a static SQL statement and return the results it produces
By default, only one ResultSet object can be opened per Statement object at a time
2. Execute execute methods (executeUpdate, executeQuery)
ExecuteUpdate (String sql)
Executes a given SQL statement, which may be an INSERT, UPDATE, or DELETE statement, or an SQL statement that returns nothing (such as a SQL DDL statement)
The return value of the method:
The number of rows of data affected after the execution of the SQL statement
(1) for SQL data manipulation language (DML) statements, return row count
(2) for SQL statements that return nothing, return 0
ExecuteQuery (String sql)
Executes the given Query statement statement that returns a single ResultSet object (result set).
ResultSet
A data table that represents the result set of a database, usually generated by executing statements that query the database.
The returned result set needs to be parsed
3. Parsing ResultSet objects (general query operations only exist)
Data storage format in ResoultSet objects: 2D tabl
Traversing the result set method:
The ResultSet object has a row cursor pointing to its current data row.
Initially, the cursor is placed before the first line.
The next method moves the cursor to the next line; because it returns false when the ResultSet object has no next row, it can be used in the while loop to iterate over the result set
Next ()
Move the cursor down one line from the current position.
There are two ways to get column values:
1. Column number
GetInt (int columnIndex)
2. Column name
GetInt (String columnLabel)
4. Close the connection (Connection, Statement, ResultSet)
Connection.close ();-must be closed
Statement.close ()
Resultset.close ()
Save resources
Without database connection pooling, statement and resoultset automatically shut down when connection.close () is closed
But if database connection pooling is used, connection.close () will not close the connection, but will return it to the connection pool, and the statement and resoultset objects will continue to hold
So it's best to close ResultSet, Statement and Connection sequentially.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.