Introduction to JDBC and DML, DQL operation

What is JDBC?

Java API 1. is a method for executing SQL statements

2. It consists of a set of classes written in the Java language and interfaces

3.JDBC provides a standard method of operating a data

4.JDBC goal is to make Java programmers can connect to any JDBC driver provides a JDBC database system

 

By driving connection

 

 

 

The JDBC API

1. java.sql package is the JDBC API.

2. The major database vendors will provide implementation classes for the JDBC API - driver package

3. Be careful not to mistake the lead pack, do not refer to this class not to introduce com.mysql..Xxx

Java connect to MySQL

Add Drive

1. Create a file folder in which the project is lib

2. Copy Mysql driver package to the folder

3.builder path compiler path

Load the driver

The com.mysql.jdbc.Driver The JVM byte code loaded into

When a bytecode is loaded into the JVM, the static block of code will execute bytecode

JDK1.6 from the start without having to manually load the driver

 

Code Example:

 

 

 

Create a table operation MySql

1. Driver loading

2. Connect database

3. Create sql statement to be executed

Statement interface for executing a static SQL statement object

SQL statements sent to the database for execution. And returns the result of execution

1. For DQL query returns a result set

2. For DML returns the number of rows affected

3. For DDL returns 0

executeUpate(String)

DDL and DML statements execution

4. Execute the sql

The release of data resources

 

 

 

DML , query operation

DML operations and DDL operations are the same, only the sql statement occurred variables

Result set

ResultSet: shows the results of a database query of collections, in case of a query will get a result like this

Common method

boolean next (): determine whether there is a next row of data, if so, the cursor moves down one line.

getXxx (int columnIndex): Gets the current line, the first of several columns (starting at 1): not recommended

getXxx (String columnName): Gets the current row, column name specified column value .columnName a column name alias / column

If the column type is VARCHAR / CHAR / TEXT, use getString to get the value of the column.

If the column type is int / integer / -> getInt to get the value of the column.

Sql execution

executeQuery (Sql) will get a result set

Get a data:

 

 

 

Gets a row of data:

 

 

 

Get multiple rows of data

 

Before that I used 5.0.13 of the driver package, use the back of a driver package 8.0.15

 

Old nine school community members produced

Guess you like

Origin www.cnblogs.com/ljxt/p/11608583.html