JDBC basic learning (1)

1. Related concepts

1. What is JDBC?

When we are learning JAVA\C\C++ and other programming languages, if you want to store some data, you can use Array, List, Map and other types of data structures to store, but once the program is executed, the data is released along with the memory, then we You will think that you can write data into a file of a specified type for permanent line storage. Compared with the former, the medium of data storage is already good, but there are still many inconveniences when complex data structures are stored in files and used. At this point, the database file system appears. A specialized application software for managing data. And for this kind of data management, the SQL language is produced. Operating the database through SQL statements greatly improves the efficiency of our data operations. But in actual development, SQL statements are generally hosted in our programming language. However, there are many kinds of programming languages. If each language has its own corresponding operation process, it is not conducive to improving the development efficiency. Therefore, it is necessary to formulate a unified operation database standard, which is the reason for the emergence of JDBC. JDBC is equivalent to providing a set of standards for the host language to operate the database.
Simply put, JDBC (Java Data Base Connectivity, java database connection) is a Java API for executing SQL statements, which can provide unified access to a variety of relational databases. It consists of a set of classes and interfaces written in the Java language. JDBC provides a benchmark upon which to build more advanced tools and interfaces that enable database developers to write database applications.

 

2. Database driven?

Although JDBC has established standards, there are differences between databases provided by different manufacturers. Therefore, under the rules of JDBC, major database manufacturers implement the interface standards of JDBC and expose interfaces for use. Therefore, it is biased towards developer development. Developers only need to develop according to the JDBC standard. When operating a certain manufacturer's database, they only need to introduce the driver package provided by the third-party database manufacturer.

 

2. Commonly used interfaces in JDBC

1. Driver interface  

The Driver interface is provided by the database manufacturer. As a Java developer, you only need to use the Driver interface. To connect to the database in programming, the database driver of a specific manufacturer must be loaded first, and different databases have different loading methods. For example:
  Load MySql driver: Class.forName("com.mysql.jdbc.Driver");
  Load Oracle driver: Class.forName("oracle.jdbc.driver.OracleDriver");

2.Connection interface

Connection A connection (session) to a specific database, executes sql statements in the context of the connection and returns results. The DriverManager.getConnection(url, user, password) method is based on the database Connection defined in the JDBC URL.
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(): Create a statement object that sends SQL to the database.
prepareStatement(sql) : Creates a PrepareStatement object that sends precompiled sql to the database.
prepareCall(sql): Creates a callableStatement object that executes the stored procedure.
setAutoCommit(boolean autoCommit): Set whether the transaction is automatically committed.
commit() : Commits the transaction on the link.
rollback() : Rollback the transaction on this link.

 

3.Statement interface

An object used to execute a static SQL statement and return the result it produces.
Three Statement classes:
Statement: Created by createStatement, used to send simple SQL statements (without parameters).
PreparedStatement: Inherited from the Statement interface, created by preparedStatement, used to send SQL statements with one or more parameters. PreparedStatement objects are more efficient than Statement objects and can prevent SQL injection, so we generally use PreparedStatement.
CallableStatement: Inherited from the PreparedStatement interface, created by the method prepareCall, and used to call the stored procedure.
Common Statement methods:
execute(String sql): Run the statement and return whether there is a result set
executeQuery(String sql): Run the select statement and return the ResultSet result set.
executeUpdate(String sql): Run the insert/update/delete operation and return the number of rows updated.
addBatch(String sql) : Put multiple sql statements into a batch.
executeBatch(): Send a batch of SQL statements to the database for execution.

4. ResultSet interface

ResultSet provides methods for retrieving fields of different types, commonly used are:
getString(int index), getString(String columnName): Obtain data objects of varchar, char and other types in the database.
getFloat(int index), getFloat(String columnName): Get the data object of type Float in the database.
getDate(int index), getDate(String columnName): Get data of type Date in the database.
getBoolean(int index), getBoolean(String columnName): Get Boolean type data in the database.
getObject(int index), getObject(String columnName): Get any type of data in the database.
ResultSet also provides a method to scroll the result set:
next(): move to the next row
Previous(): move to the previous row
absolute(int row): move to the specified row
beforeFirst(): move the front of the resultSet.
afterLast() : Move to the end of the resultSet.
Close the object and connection in turn after use: ResultSet → Statement → Connection

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326465317&siteId=291194637