数据库实验知识要点.doc

数据库实验知识要点课案

执行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

文档评论(0)

1亿VIP精品文档

相关文档