首页 > 编程 > JSP > 正文

网上找到的JSP联接Oracle的原码

2024-09-05 00:19:27
字体:
来源:转载
供稿:网友


收集最实用的网页特效代码!


import java.io.reader;
import java.sql.*;
import javax.servlet.servletrequest;
import javax.servlet.http.httpservletrequest;
import oracle.jdbc.driver.oracleresultset;
import oracle.sql.clob;
public class db_sql
{
/**
* 构造函数,new db_sql 的时候执行,调用 connect() 连接oracle数据库
*/
public db_sql(string s, string s1, string s2, string s3, string s4)
throws exception
{
isclosed = false;
host = s.trim();
port = s1.trim();
sid = s2.trim();
user = s3.trim();
password = s4.trim();
connmgr = dbconnectionmanager.getinstance();
connect();
}
/**
* 连接oracle数据库
*/
public boolean connect()
throws exception
{
string s = "jdbc:oracle:thin:@" + host + ":" + port + ":" + sid;
conn = connmgr.getconnection(user, s, user, password);
return true;
}
/**
* 是否自动 commit
*/
public void setautocommit(boolean flag)
throws sqlexception
{
bautocommit = flag;
conn.setautocommit(flag);
}
/**
* 没有设置成自动 commit ,调用该方法才会 commit
*/
public void commit()
throws sqlexception
{
if(!bautocommit)
conn.commit();
}
/**
* 没有设置成自动 commit ,调用该方法才会 roll back
*/
public void rollback()
throws sqlexception
{
if(!bautocommit)
conn.rollback();
}
/**
* 执行 sql ,返回执行结果 true/false
*/
public resultset query(string s)
throws exception
{
if(stmt == null)
stmt = conn.createstatement();
if(result != null)
{
result.close();
result = null;
}
result = stmt.executequery(s);
return result;
}
public void querylarge(string s, string s1)
throws exception
{
stmt.execute(s);
resultset resultset = stmt.getresultset();
if(resultset.next())
{
clob clob = ((oracleresultset)resultset).getclob(1);
clob.putchars(1l, s1.tochararray());
}
resultset.close();
}
/**
* 把结果集里的指针下移一位
*/
public boolean next()
throws sqlexception
{
return result.next();
}
/**
* 取得当前记录的 int 类型字段值,前后去空格
*/
public int getint(string s)
throws sqlexception
{
return result.getint(s.trim());
}
/**
* 取得当前记录的 string 类型字段值,前后去空格
*/
public string getstring(string s)
throws sqlexception
{
return result.getstring(s.trim());
}
/**
* 取得当前记录的 short 类型字段值,前后去空格
*/
public short getshort(string s)
throws sqlexception
{
return result.getshort(s.trim());
}

/**
* 取得当前记录的 long 类型字段值,前后去空格
*/
public long getlong(string s)
throws sqlexception
{
return result.getlong(s.trim());
}

/**
* 取得当前记录的 date 类型字段值,前后去空格
*/
public date getdate(string s)
throws sqlexception
{
return result.getdate(s.trim());
}

/**
* 取得当前记录的 time 类型字段值,前后去空格
*/
public time gettime(string s)
throws sqlexception
{
return result.gettime(s.trim());
}
/**
* 取得当前记录的 float 类型字段值,前后去空格
*/
public float getfloat(string s)
throws sqlexception
{
return result.getfloat(s.trim());
}
/**
* 取得当前记录的 double 类型字段值,前后去空格
*/
public double getdouble(string s)
throws sqlexception
{
return result.getdouble(s.trim());
}
/**
* 取得当前记录的 boolean 类型字段值,前后去空格
*/
public boolean getboolean(string s)
throws sqlexception
{
return result.getboolean(s.trim());
}
/**
* 取得当前记录的 clob 类型字段值
*/
public string gettext(string s)
throws sqlexception
{
string s1 = "";
char ac[] = new char[200];
clob clob = (clob)result.getobject(s);
if(clob == null)
return null;
reader reader = clob.getcharacterstream();
int i;
try
{
while((i = reader.read(ac, 0, 200)) != -1)
s1 = s1 + new string(ac, 0, i);
}
catch(exception exception1)
{
throw new sqlexception(exception1.getmessage());
}
finally
{
try
{
reader.close();
}
catch(exception _ex) { }
}
return s1;
}
/**
* 关闭数据库连接,执行 commit,release 动作
*/
public boolean close()
throws sqlexception
{
if(result != null)
{
result.close();
result = null;
}
if(stmt != null)
{
stmt.close();
stmt = null;
}
conn.setautocommit(true);
connmgr.freeconnection(user, conn);
connmgr.release(user);
isclosed = true;
return true;
}
/**
* 没有调用 close() 时,执行 close()
*/
protected void finalize()
throws sqlexception
{
if(!isclosed)
close();
}
/**
* 取得 http 参数值,所有得到的值都做了
* string (request.getparameter(s.trim()).trim().getbytes("iso8859_1"), "gb2312") 处理
*/
public static string getparameter(httpservletrequest httpservletrequest, string s)
{
try
{
if(httpservletrequest.getparameter(s.trim()) != null)
return new string(httpservletrequest.getparameter(s.trim()).trim().getbytes("iso8859_1"), "gb2312");
else
return null;
}
catch(exception _ex)
{
return httpservletrequest.getparameter(s.trim());
}
}
private string host;
private string port;
private string database;
private string user;
private string password;
private string sid;
private boolean bautocommit;
public resultset result;
public statement stmt;
public int affectedrows;
public connection conn;
private boolean isclosed;
private dbconnectionmanager connmgr;
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表