Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of JDBC technology 1

2025-05-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Jdbc is a very important technology in database, and it is the most commonly used interface implementation class in our project.

Here are just some friends to have a brief and detailed chat.

First of all, we should know what jdbc is.

JDBC (JavaDataBaseConnectivity) java database connection is a technical specification under the JavaEE platform, which defines the standard of connecting data and executing SQL statements in Java language, which can provide unified access for a variety of relational databases.

But if you want to access the database, you must use the database driver.

So what is a database driver?

According to the specific implementation of JDBC specification by database manufacturers, the database driver names of different data products are different, so they need to rely on database driver to complete the operation of the database in the program.

We are now using the JDBC3.0 standard

The commonly used interfaces are

1 Driver interface

The purpose of the Driver interface is to define some of the capabilities that database-driven objects should have. For example, the definition of the method of establishing a connection with a database all databases that support java language connections implement this interface, and the classes that implement the interface are called database-driven classes. In order to connect to the database in the program, we must first load the database driver class through JDK's reflection mechanism and instantiate it. Different database-driven classes have different class names. Load MySql driver: Class.forName ("com.mysql.jdbc.Driver"); load Oracle driver: Class.forName ("oracle.jdbc.driver.OracleDriver")

DriverManager class

DriverManager

Through instantiated database-driven objects, a connection can be established between the application and the database. And returns a database connection object of type Connection.

Common methods

GetConnection (StringjdbcUrl,Stringuser,Stringpassword)

This method returns the Connection object of the corresponding database by accessing the url, user and password of the database.

JDBCURL

Used to connect to the specified database identifier when connecting to the database. The type, address, port, library name and other information of the database are included in URL. The connection URL of different brand databases is different.

Connection interface

The connection (session) object between Connection and the database. We can execute the sql statement and return the knot through this object

Fruit.

Connect to MySql database: Connection conn = DriverManager.getConnection ("jdbc:mysql://host:port/database", "user", "password"); connect to Oracle database:

Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@host:port:database", "user", "password"); connect to SqlServer database: Connection conn = DriverManager.getConnection ("jdbc:microsoft:sqlserver://host:port; DatabaseName=database", "user", "password")

Common methods

CreateStatement (): creates an object of the Statement interface type that sends sql to the database.

PreparedStatement (sql): object that creates a PrepareSatement interface type that sends precompiled sql to the database

Object.

PrepareCall (sql): creates an object of the CallableStatement interface type that executes the stored procedure.

SetAutoCommit (booleanautoCommit): sets whether the transaction commits automatically. Commit (): commit the transaction on the link.

Rollback (): rolls back the transaction on this link.

Statement interface

The object used to execute a static SQL statement and return the results it generates. Created by createStatement to send simple SQL statements (dynamic binding is not supported).

Common methods

Execute (String sql): executes the SQL in the parameter and returns whether there is a result set. ExecuteQuery (Stringsql): runs the select statement and returns the ResultSet result set. ExecuteUpdate (Stringsql): runs the insert/update/delete operation and returns the number of updated rows.

AddBatch (Stringsql): put multiple sql statements into a batch. ExecuteBatch (): sends a batch of sql statements to the database for execution.

PreparedStatement interface

Inherited from the Statement interface and created by preparedStatement, it is used to send SQL statements with one or more parameters. PreparedStatement objects are more efficient than Statement objects and prevent SQL injection, so we generally use PreparedStatement.

Common methods

AddBatch () adds the current sql statement to a batch.

Execute () executes the current SQL and returns a boolean value

ExecuteUpdate () runs the insert/update/delete operation and returns the number of rows updated.

ExecuteQuery () executes the current query and returns a result set object

SetDate (intparameterIndex,Date x) binds a java.sql.Date to the specified location in the current SQL statement

Value.

SetDouble (int parameterIndex, double x) binds a double to the specified location in the current SQL statement

Value

SetFloat (intparameterIndex,floatx) binds a float value to the specified location in the current SQL statement

SetInt (intparameterIndex,intx) binds an int value to the specified location in the current SQL statement

SetString (intparameterIndex,Stringx) binds a String value to the specified location in the current SQL statement

ResultSet interface

ResultSet provides methods for retrieving different types of fields.

Common methods

GetString (intindex), getString (StringcolumnName) get varchar, char and other types of data objects in the database. GetFloat (intindex) and getFloat (StringcolumnName) get data objects of type Float in the database. GetDate (intindex) and getDate (StringcolumnName) get data of type Date in the database.

GetBoolean (intindex) and getBoolean (StringcolumnName) get data of type Boolean in the database.

GetObject (intindex), getObject (StringcolumnName) get any type of data in the database.

The method of scrolling the result set by ResultSet

Next (): move to the next line.

Previous (): move to the previous line.

Absolute (introw): moves to the specified line.

BeforeFirst (): move the front of the resultSet.

AfterLast (): move to the back of the resultSet.

CallableStatement interface

Inherited from the PreparedStatement interface, created by the method prepareCall, it is used to call the stored procedure of the database.

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: 295

*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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report