Beginner JDBC

JDBC

JDBC Overview

JDBC is a java API for executing a Java statement can provide uniform access to a variety of relational databases, he has written java class consists of a set and interface. Is java database access standards.

JDBC principle

First clear the two concepts

  1. JDBC: specification is connected to the database provided java
  2. Drive: the interface class is implemented, no drive can not complete the connection to the database, it can not achieve the operation of the database, in response to the drive implementations normally provided by the respective database vendor.
  3. Schematic

Here Insert Picture Description

JDBC database operation step

  1. Registration drive
Class.forName(“com.mysql.jdbc.Driver”);
  1. GETTING CONNECTED
String url="jdbc:mysql://localhost:3306/member";//子协议:子名称//主机名:port/数据库名
String username="root";//数据库的username
String password="root";//数据库密码
Connection conn = DriverManager.getConnection(url, username, password);
  1. SQL statement is executed to obtain the object
    acquired by the Connection object Statement tool, this approach has the risk of SQL injection attacks.
    Get Connection object PreparedStatement tools, PreparedStatement subclass of Statement, SQL statements, and stored in a precompiled
    PreparedStatement object, may then be performed as many times this statement using the object, and also to prevent SQL injection.
PreparedStatement  ps=connection.prepareStatement( "update user set id=?  where username=?”);“?”为占位符 
setXXX(int parameterIndex,String x) :可通过此方法可设置各占位符变量的值,其中paremeterIndex为第 paremeterIndex各变量的
	值。而且预编译结果能够存储在PreparedStatement对象中。当多次运行SQL语句时能够提高效率。
  1. Execute SQL statements
int n = prst.executeUpdate();;//执行在该SQL语句PreparedStatement对象,它必须是一个SQL数据操纵语言(DML)语句,如INSERT , UPDATE或DELETE ; 或不返回任何内容的SQL语句,例如DDL语句
ResultSet re = prst.executeQuery();//执行此 PreparedStatement对象中的SQL查询,并返回查询 PreparedStatement的 ResultSet对象。 

  1. process result

executeUpdate();Variable Meaning of shaping return the number of rows in the database affected.
ResultSet result = prst.executeQuery();
When we query the database table, the result returned is usually a two-dimensional data table result set. I do understand it is a similar set of iterators and often use it to traverse the query results.
DETAILED query is: ResultSet object remains a cursor to its current data row. Initially, the cursor is positioned before the first row. The method next moves the cursor to the next line, and since there are no more rows returns false, it is possible to traverse the result set cycles are used in the while loop ResultSet object.

获取结果集数据有两种常用方式

result.getXXX(int columnIndex):// Get the current data specified index value of the line
result.getXXX(String columnLabel);// get the data column name specified database table in the current line

  1. Close the resource
    resources must be freed after use.
JDBC encountered several errors
  1. result.getString (String label); the label writing is not standardized, inconsistent with the column names of tables in the database, run the Times Column 'id' not Found
  2. SQL statement clerical error, after the SQL statement you want to check under the write code
  3. SQL statement and Date format includes types of inserts '2018-12-23'or 20181223, wherein the second should be enclosed in single quotes
Think
  1. In fact each "get connected" and "close connection" to the two processes consuming system resources, then how to solve
    analysis: you can use connection pooling, the connection unified maintenance, do not always close the connection establishment and
    connection pool implementation reference: https://blog.csdn.net/qq_41345281/article/details/89071128

Published 24 original articles · won praise 5 · Views 3948

Guess you like

Origin blog.csdn.net/qq_41345281/article/details/89042777