JDBC学习总结(一)
JDBC类型 | Java类型 |
CAHR | String |
VARCHAR | String |
LONGVARCHAR | String |
NUMERIC | java.math.BigDecimal |
DECIMAL | java.math.BigDecimal |
BIT | Boolean |
BOOLEAN | Boolean |
TINYINT | byte |
SMALLINT | short |
INTEGET | int |
BIGINT | long |
REAL | float |
FLOAT | double |
BOUBLE | double |
BINARY | byte[] |
VARBONARY | byte[] |
LONGVARBINARY | byte[] |
DATE | java.sql.Date |
TIME | java.sql.Time |
TIMESTAMP | java.sql.Timestamp |
CLOB | Clob |
BLOB | Blob |
ARRAY | Array |
DISTINCT | mapping of underlying type |
STRUCT | struct |
REF | Ref |
DATALINK | java.net.URL |
JAVA_OBJECT | underlying Java class |
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
System.out.println("找不到驱动程序类");
e.printStackTrace();
}
jdbc:mysql://localhost:3306/test?userUnicode=true&characterEncoding=gbk.
String url = "jdbc:mysql://localhost:3306/test?userUnicode=true&characterEncoding=gbk";
String userName = "root";
String password = "123456";
try{
Connection conn = DriverManager.getConnection(url,userName,password);
}catch(SQLException e){
System.out.println("Failed to get connection: "+e.getMessage());
e.printStackTrace();
}
Statement stmt = conn.createStatement();
PreparedStatement pstmt = conn.preparedStatement(sql);
CallableStatement cstmt = conn.prepareCall("{CALL demoSp(?,?)}");
5)执行SQL语句
ResultSet rs = stmt.executeQuery("SELECT * FROM ...");
int rows = stmt.executeUpdate("INSERT INTO ...");
boolean flag = stmt.execute(String sqlString);
String name = rs.getString("name");
String pass = rs.getString(1);
finally{
//关闭记录集
if (null != rs){
try{
rs.close();
}catch (SQLException e){
e.printStackTrace();
}
}
//关闭声明
if (null != stmt){
try {
stmt.close();
}catch (SQLException e){
e.printStackTrace();
}
}
//关闭连接对象
if (null != conn){
try{
conn.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
conn.setAutoCommit(false);
2)将需要添加事务的代码放在try、catch块中:
try{
//需要添加事务的业务代码
...
}catch(SQLException e){
...
}
3)在try块内添加提交操作,表示操作无异常,提交事务:
conn.commit();
4)在catch块内添加回滚事务,表示操作出现异常,撤销事务:
conn.rollback();
5)设置事务提交方式为自动提交:
conn.setAutoCommit(true);
在JDBC处理事务的过程中,也可以设置事务的回滚点,当事务回滚的时候,自动回滚到保存点。
Savepoint savepoint = conn.setSavepoint();
conn.rollback(savepoint);
stmt.releaseSavepoint(savepoint);
注意:如要数据表支持事务,则在MySQL中建立的表类型为InnoDB。
package com.yyq;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* Created by gao on 16-4-12.
*/
public class Transaction {
public static final String Driver = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost:3306/test";
public static final String USER_NAME = "root";
public static final String PASSWORD = "123456";
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "INSERT INTO student(name,score,class) values(?,null,null)";
String sql2 = "delete from student where id = 69";
try {
Class.forName(Driver);
conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,"testing");
System.out.println("第一条语句执行....");
pstmt.executeUpdate();
pstmt = conn.prepareStatement(sql2);
System.out.println("第二条语句执行....");
pstmt.executeQuery();
conn.commit();
System.out.println("提交事务");
}catch (ClassNotFoundException e){
System.out.println("找不到驱动程序类");
e.printStackTrace();
}catch (SQLException e){
try{
conn.rollback();
System.out.println("回退事务....");
e.printStackTrace();
}catch (SQLException e1){
e1.printStackTrace();
}
}finally {
try{
conn.setAutoCommit(true);
}catch (SQLException e){
e.printStackTrace();
}
if (null != pstmt){
try{
pstmt.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if (null != conn){
try {
conn.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
}
}
try {
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
stmt.addBatch("....");
stmt.addBatch("....");
.....
stmt.executeBatch();
conn.commit();
}catch (SQLException e){
try {
conn.rollback();
}catch (SQLException e1){
e1.printStackTrace();
}
e.printStackTrace();
}finally {
try {
conn.setAutoCommit(true);
}catch (SQLException e){
e.printStackTrace();
}
//关闭资源
}
JDBC学习总结(一)的更多相关文章
- JDBC学习笔记(2)——Statement和ResultSet
Statement执行更新操作 Statement:Statement 是 Java 执行数据库操作的一个重要方法,用于在已经建立数据库连接的基础上,向数据库发送要执行的SQL语句.Statement ...
- JDBC学习笔记(1)——JDBC概述
JDBC JDBC API是一个Java API,可以访问任何类型表列数据,特别是存储在关系数据库中的数据.JDBC代表Java数据库连接. JDBC库中所包含的API任务通常与数据库使用: 连接到数 ...
- 【转】JDBC学习笔记(2)——Statement和ResultSet
转自:http://www.cnblogs.com/ysw-go/ Statement执行更新操作 Statement:Statement 是 Java 执行数据库操作的一个重要方法,用于在已经建立数 ...
- 【转】JDBC学习笔记(1)——JDBC概述
转自:http://www.cnblogs.com/ysw-go/ JDBC JDBC API是一个Java API,可以访问任何类型表列数据,特别是存储在关系数据库中的数据.JDBC代表Java数据 ...
- jdbc学习总结
jdbc学习总结: 一.简介: jdbc,直译为java连接数据库.实际为java为很好的操作数据库而提供的一套接口,接口的实现(即驱动)由各个数据库厂商提供. 二.知识要点: 连接5要素,3 ...
- JDBC 学习笔记(十一)—— JDBC 的事务支持
1. 事务 在关系型数据库中,有一个很重要的概念,叫做事务(Transaction).它具有 ACID 四个特性: A(Atomicity):原子性,一个事务是一个不可分割的工作单位,事务中包括的诸操 ...
- JDBC 学习笔记(十)—— 使用 JDBC 搭建一个简易的 ORM 框架
1. 数据映射 当我们获取到 ResultSet 之后,显然这个不是我们想要的数据结构. 数据库中的每一个表,在 Java 代码中,一定会有一个类与之对应,例如: package com.gerrar ...
- JDBC 学习笔记(六)—— PreparedStatement
1. 引入 PreparedStatement PreparedStatement 通过 Connection.createPreparedStatement(String sql) 方法创建,主要用 ...
- JDBC学习笔记二
JDBC学习笔记二 4.execute()方法执行SQL语句 execute几乎可以执行任何SQL语句,当execute执行过SQL语句之后会返回一个布尔类型的值,代表是否返回了ResultSet对象 ...
- JDBC学习笔记一
JDBC学习笔记一 JDBC全称 Java Database Connectivity,即数据库连接,它是一种可以执行SQL语句的Java API. ODBC全称 Open Database Conn ...
随机推荐
- c#中的类型转换
Parse类型转换 Parse()函数 int.double都能调用Parse()函数,Parse(string str);如果转换成功就成功,失败就会抛出一个异常; TryParse()函数 相应地 ...
- MySQL主从同步报Client requested master to start replication from position
数据库版本:5.6.16 测试环境MySQL 主从,数据库被人重启,忘记开启start slave,导致主从失效,停了一天的数据没有追上. 查看从库的数据库状态:show slave stat ...
- 从InputStream到String_写成函数
String result = readFromInputStream(inputStream);//调用处 //将输入流InputStream变为String public String readF ...
- C# Windows - 菜单栏和工具栏
除了MenuStrip控件之外,还有许多控件可用于填充菜单.3个常见的控件是ToolStripMenuItem,ToolStripDropDown,和ToolStripSeparator.这些控件表示 ...
- java集合类(六)About Queue
接上篇“java集合类(五)About Map” 终于来到了java集合类的尾声,太兴奋了,不是因为可以休息一阵了,而是因为又到了开启新知识的时刻,大家一起加油打气!!Come on...Fighti ...
- 不同的source control下配置DiffMerge
TFS: 1. 打开Option -> Source Control -> Visual Studio TFS -> Configure User Tools; 2. 添加 .*, ...
- Feature Extraction
http://www.erogol.com/ml-work-flow-part-3-feature-extraction/
- ExtJs3带条件的分页查询的实现
使用ExtJs的同志们一定知道GridPanel哈~神器一般,非常方便的显示表格类型的数据,例如神马用户列表.产品列表.销售单列表.XXXX列表等.从数据库中查询所需的数据,以列表的形式显示出来,我们 ...
- 【转载】Spring中的applicationContext.xml与SpringMVC的xxx-servlet.xml的区别
一直搞不明白两者的区别. 如果使用了SpringMVC,事实上,bean的配置完全可以在xxx-servlet.xml中进行配置.为什么需要applicationContext.xml?一定必须? 一 ...
- Unity3D开发之“获取IOS设备所在的国家代码"
原地址:http://dong2008hong.blog.163.com/blog/static/469688272014021025578/ 在前一段时间游戏开发中需要实现获取IOS设备所在的国家代 ...