Some use reference about JDBC
Backup and Restore
备份: mysqldump -u 用户名 -p 密码 数据库 > 文件名
还原:
登录使用数据库
use 数据库
source 文件名
Set transaction rollback point
设置事务回滚点: savepoint 名字
回到回滚点: rollback to 名字
JDBC
JDBC is to define the nature of a set of operating rules for all relational databases (MySQL, Oracle, etc.) / interface.
Garbage processing
如果数据库出现乱码,可以指定参数: ?characterEncoding=utf8,表示让数据库以 UTF-8 编码来处理数据。
jdbc:mysql://localhost:3306/数据库?characterEncoding=utf8
Release resources
1) 需要释放的对象: ResultSet 结果集, Statement 语句, Connection 连接
2) 释放原则:先开的后关,后开的先关。 ResultSet Statement Connection
3) 放在哪个代码块中: finally 块
Method ResultSet interface
method | description |
---|---|
boolean next() | 1) moves the cursor down one line 2) returns a boolean type, if there is a next record, returns true, false otherwise |
GetXxx data type () | 1) by the field name, the parameter is of type String. Different types of return 2) by the column number, the parameter is an integer from 1 starts. Return a different type |
Common data type conversion table
SQL type | Corresponding method Jdbc | Return Type |
---|---|---|
BIT(1) bit(n) | getBoolean() | boolean |
TINYINT | getByte() | byte |
SMALLINT | getShort() | short |
INT | tinted() | int |
BIGINT | getLong() | long |
CHAR,VARCHAR | getString() | String |
Text(Clob) Blob | getClob getBlob() | Clob Blob |
DATE | getDate() | java.sql.Date represent date |
TIME | getTime() | java.sql.Time only that time |
TIMESTAMP | getTimestamp() | java.sql.Timestamp while the date and time |
java.sql.Date, Time, Timestamp (time stamp), three common parent class is: java.util.Date
The implementation of the principle of PrepareStatement
Statement对象每执行一条sql语句,都会先将sql语句发送给数据库,数据库先编译sql,再执行。如果有1万条类似的sql语句,数据库需要编译1万次,执行1万次,效率低。
PrepareStatement会先将sql语句发送给数据库预编译。PrepareStatement会引用预编译后的结果,可以多次传入不同的参数给PrepareStatement对象,并执行。如果有1万条类似的sql语句,数据库只需要编译1次,传入1万次不同的参数并执行,减少了sql语句的编译次数,提高了执行效率。还可以有效防止sql注入问题,安全性更高。
JDBC Transactions
API
Connection interface associated with the transaction method | Explanation |
---|---|
void setAutoCommit(boolean autoCommit) | Argument is true or false If set to false, indicating that close automatically submitted, the equivalent of open transactions |
void commit() | Commit the transaction |
void rollback() | Roll back the transaction |