数据库实验知识要点课案
执行JDBC 执行SQL的整体流程:
加载驱动:Class.forName(“……”)//注意try...catch
获取连接:DriverManager.getConnection(url,psw,psw);
创建Statement/PreparedStatement:con.createStatement();或者con.prepareStatement(sql,...,...);
执行SQL语句: (PreparedStatement )pret.setString(index,value);pret.executeQuery();pret.executeUpdate(); (Statement)stmt.executeUpdate(sql)/stmt.executeQuery(sql);
如果是Query,返回结果集Result,对结果集进行处理,如果是Update,返回SQL语句影响数据条数。
关闭连接释放资源。
对Connection的处理:
主要有三个:
设置自动提交:con.setAutoCommit(false);
提交:mit();
回滚:con.rollback();
关闭:con.close();
对Statement的处理:
主要有三种处理:查询、修改(增删改)、批处理:
查询:
stmt.executeQuery(sql);//sql为SQL语句
修改:
stmt.executeUpdate(sql);//sql为SQL语句
批处理:
stmt.addBatch(sql);//sql为单条SQL语句
stmt.executeBatch();
注意:Statement使用完后需要关闭:stmt.close();
对PreparedStatement的处理
PreparedStatement运行的速度比Statement快,使用方式也比Statement繁琐。网上有评论说稍微有点经验的程序员都应该不使用Statement而使用PreparedStatement。
首先从PreparedStatement的创建讲起:
PreparedStatement的创建主要有两种方式:
prepareStatement(String?sql) sql通常是由占位符‘?’的sql语句 prepareStatement(String?sql, int?resultSetType, int?resultSetConcurrency)
resultSetType是结果集类型,
static int
TYPE_FORWARD_ONLY
The constant indicating the type for a ResultSet object whose cursor may move only forward.
static int
TYPE_SCROLL_INSENSITIVE
The constant indicating the type for a ResultSet object that is scrollable but generally not sensitive to changes to the data that underlies the ResultSet.
static int
TYPE_SCROLL_SENSITIVE
The constant indicating the type for a ResultSet object that is scrollable and generally sensitive to changes to the data that underlies the ResultSet.
TYPE_FORWORD_ONLY,只可向前滚动; 2.TYPE_SCROLL_INSENSITIVE,双向滚动,但不及时更新,就是如果数据库里的数据修改过,并不在ResultSet中反应出来。 3.TYPE_SCROLL_SENSITIVE,双向滚动,并及时跟踪数据库的更新,以便更改ResultSet中的数据。
resultSetConcurrency:结果集并发性。
static int
CONCUR_READ_ONLY
The constant indicating the concurrency mode for a ResultSet object that may NOT be updated.
static int
CONCUR_UPDATABLE
The cons
您可能关注的文档
- 大班幼儿容易出现的问题.pptx
- 数据库医药管理系统.doc
- 大班科学:温度计.ppt
- 大班第十八周教学计划.doc
- 数据库原理——各章习题课案.doc
- 大班组体育大课间平衡活动方案课件.doc
- 数据库原理与应用-复习题课案.doc
- 数据库中间件高级技术支持服务说明课案.doc
- 数据库原理与应用-期末测试题课案.doc
- 数据库原理与应用2.doc
- 2025年全国演出经纪人员资格认定考试试卷带答案(研优卷).docx
- 2025年全国演出经纪人员资格认定考试试卷完整版.docx
- 2025年全国演出经纪人员资格认定考试试题库及完整答案.docx
- 2025年全国演出经纪人员资格认定考试试卷完美版.docx
- 2025年全国演出经纪人员资格认定考试试卷含答案(实用).docx
- 2025年全国演出经纪人员资格认定考试试卷及答案(各地真题).docx
- 2025年下半年内江市部分事业单位公开考试招聘工作人员(240人)备考题库附答案.docx
- 2025年全国演出经纪人员资格认定考试试卷及答案1套.docx
- 2025年下半年四川成都市郫都区面向社会引进公共类事业单位人员2人备考题库最新.docx
- 2025年下半年内江市部分事业单位公开考试招聘工作人员(240人)备考题库附答案.docx
原创力文档

文档评论(0)