Complete JDBC connection database code and steps in java development
JDBC connection to the database • creating a program that connects to the database with JDBC includes seven steps: 1. Loading the JDBC Driver: before connecting to the database, first load the driver of the database you want to connect to the JVM (Java virtual machine), which is realized through the static method forname (string classname) of java.lang.class class. For example:
After loading successfully, an instance of the driver class will be registered in the drivermanager class. 2. Provide the URL of JDBC connection • the connection URL defines the protocol, sub protocol and data source ID when connecting to the database. • Writing form: protocol: sub protocol: data source identification protocol: always start with JDBC in JDBC. Sub protocol: is the driver of bridge connection or the name of database management system. Data source ID: mark the address and connection port where the database source is found. For example: (connection URL of MySQL) JDBC: MySQL: / / localhost: 3306 / test? Useunicode = true & characterencoding = GBK; useunicode = true: indicates the Unicode character set is used. If characterencoding is set to GB2312 or GBK, this parameter must be set to true. Characterencoding = GBK: character encoding method.
3. Create a connection to the database
• to connect to the database, you need to apply to Java sql. The drivermanager requests and obtains a connection object, which represents a connection to a database. • Use the getconnect in (string URL, string username, string password) method of drivermanager to pass in the path of the specified database to be connected, the user name and password of the database. For example:
4. Create a statement
• to execute SQL statements, you must obtain Java sql. Statement instance, which is divided into the following three types: 1. Execute static SQL statements. It is usually implemented through a statement instance. 2. Execute dynamic SQL statements. It is usually implemented through the Preparedstatement instance. 3. Execute database stored procedures. It is usually implemented through a callablestatement instance. Specific implementation method:
5. Execute SQL statement
The statement interface provides three methods for executing SQL statements: executeQuery, executeupdate, and execute. 1. Resultset executeQuery (string sqlstring): executes SQL statements that query the database and returns a resultset object. 2. Int executeupdate (string sqlstring): executes insert, update, or delete statements and SQL DDL statements, For example, create table, drop table, etc. 3. Execute (sqlstring): used to execute statements that return multiple result sets, multiple update counts, or a combination of the two. Specific implementation code:
6. Processing results
There are two situations: 1. The number of records affected by this operation is returned after updating. 2. The result returned by executing the query is a resultset object. • The resultset contains all rows that meet the conditions in the SQL statement, and it provides access to the data in these rows through a set of get methods. • Get data using the access method of the resultset object:
(columns are numbered from left to right and start with column 1)
7. Close JDBC object
After the operation is completed, close all used JDBC objects to release JDBC resources. The closing sequence is opposite to the declaration sequence: 1. Close the recordset 2. Close the declaration 3. Close the connection object
Example code:
Operation effect:
id : 3 name : hongten password : 123