系统临时表的使用:
在一些程序中,我们需要一些仅仅在程序中使用的临时表,但是程序结束后就不再使用,数据库系统对这些应用提供系统临时表,关于系统临时表的生命存在周期和临时表的数据存在周期说明一下:
1、生命存在周期:
a、理论上系统临时表是不需要显示的drop的,它是基于会话的,当系统临时表基于的连接关闭的时候,系统临时表将结束它的生命,这是最普通也是最常用的。
b、当一个被调用对象的返回值是一个在临时表上执行的结果集,这个被调用对象执行完毕的时候,是关闭连接的,但是,这时候临时表是不消失的,这时候系统临时表在调用者结束的时候才消失。因为返回的结果集只是一个相当于指针的东西,指向临时表内存中的地址,指向临时表的指针还在使用的时候,临时表是不能drop掉的。这带来了很复杂的问题,首先:存储过程中是不能drop临时表的,而程序中没有创建临时表,也应该是不drop的,临时表什么时间drop呢?其次:我们在一个连接中,无法2次调用一个存储过程,他将告诉你临时表已经存在。可以做一个简单的例子:写一个带有临时表返回结果的存储过程,在调用存储存储过程的程序中,我们完全可以访问这个临时表。
简单例子:
存储过程
/**
* jdbc 存储过程 administrator.p1
*/
import java.sql.*; // jdbc 类
public class p1
{
public static void p1 ( resultset[] rs throws sqlexception, exception
{
// 获取与数据库的连接
connection con = drivermanager.getconnection("jdbc:default:connection");
preparedstatement stmt = null;
string sql;
sql = "declare global temporary table session.temp(cc char(5)) not logged";
stmt = con.preparestatement( sql ;
stmt.executeupdate();
sql = "insert into session.temp values ('1'),('2')";
stmt = con.preparestatement( sql ;
stmt.executeupdate();
sql = "select * from session.temp";
stmt = con.preparestatement( sql ;
rs[0] = stmt.executequery();
//关闭连接(注意,这个地方在rs[0]为临时表的结果集返回的时候,stmt是无法关闭的,临时表是无法drop的,而con是可以关闭的,关闭后临时表还存在)
if (con != null) con.close();
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器