1、下载Oracle的驱动ojdbc14.jar
r~4RbBWP@;X02、把ojdbc14.jar import工程的lib目录
I8?r.f%xV qD03、记得重启tomcat再进行测试,否则加载驱动失败的,51Testing软件测试网"p t|$d!z
51Testing软件测试网&r2lsJ
S
java操作Oracle数据库的类51Testing软件测试网:XB UByhg{)CU5YN!m`
package JavaClass;
v:KsP9iMy_]0/**
8W~}[,b\)cwj@mB0* Title: 数据库的访问统一接口51Testing软件测试网:Y$CO6S\n/rh#s
* Descrīption: 目的:实现对数据库的访问和操作统一接口
tL`V:Z0O0*/
6oRFw;q
DJ|z0import java.sql.*;51Testing软件测试网2mq-ekzX2i
public class sqlBean51Testing软件测试网ym*kS Jlk^uGtr
{
S9C3t0q.WvY~0//定义连接对象51Testing软件测试网aVmEv
public Connection conn=null;51Testing软件测试网)\]%z(r G{
D1N-m
//定义操作数据库后返回的结果记录集
:U^w(Q.n0public ResultSet rs=null;
R3|u'o5G0z0//定义数据库属性
Z`0|+_-f"w?6j0//String DatabaseDriver=null;
5@GM8^2XE j
L8mdr0//String DatabaseConnStr=null;
;S~ UzS0/预设初始值
vLC:B%Bnwr,_A0//数据源库驱动器
5VbH NQs6F0private String DatabaseDriver="oracle.jdbc.driver.OracleDriver";51Testing软件测试网z;s.pe7G
//DataSource 数据源名称DSN51Testing软件测试网~fo"EXS uO3Y+d,r
private String DatabaseConnStr="jdbc:oracle:thin:@127.0.0.1:1521:orazj";51Testing软件测试网*D rs1M(g"|"f_d
String userName = "scott";
c,S)KCUD0String password = "tiger";51Testing软件测试网4YFst5p*T{'n
//定义方法51Testing软件测试网/w x/eS-IjIh
/*setXxx用于设置属性值;getXxx用于得到属性值*/
[3Ix"{U(`Qu0public void setDatabaseDriver(String Driver)51Testing软件测试网
M3g&o(O:r{fL
{
2A;]BM^7E"l0this.DatabaseDriver=Driver;
&Hh#XS5V2[0}51Testing软件测试网.^;o'jnPsH
public String getDatabaseDriver()
%];E;hZ[j0{
c2BG.@(E%Ty"q0return (this.DatabaseDriver);51Testing软件测试网Yx1K$r FK
}
xa3^$?b]O1g2S0public void setDatabaseConnStr(String ConnStr)51Testing软件测试网A;i [?1xb&w2|0W.U
{
'O/jnFJ;p$qf4x\0this.DatabaseConnStr=ConnStr;
2\)Mv%yi'o/f0}51Testing软件测试网Hh%N6L%c2p(IW,R
public String getDatabaseConnStr()
6NoP})MV3fN0{51Testing软件测试网Ypp#oF2@2X9?i
return (this.DatabaseConnStr);51Testing软件测试网
qIpU9s;O
}51Testing软件测试网H'HSH q0@p*C
public sqlBean(){/构造函数51Testing软件测试网Os5T:fY5}
try{
z nd^k0//加载驱动器
o'x$c$rB0Class.forName(DatabaseDriver);51Testing软件测试网,W$mgDi&oj
System.out.print("加载驱动成功");
'J_dAS0}51Testing软件测试网*x2VD&o!{k.J
catch(java.lang.ClassNotFoundException e){
YV]4LfTk0System.err.println("加载驱动器有错误:"+e.getMessage( ));51Testing软件测试网z,w?)~TmFq
System.out.print("执行插入有错误:"+e.getMessage());//输出到客户端
!qV-E _Q4tiS.O w%_0}
!D7ZfL[,X@F
Z0}51Testing软件测试网_&s
A:{5Cm?
//MeyhodInsert,select,delete
`mbr%HGO0// insert data51Testing软件测试网2Hxc4E1yY.E,V7r
public void executeInsert(String sql)
4D'uDR-iq9];V0{51Testing软件测试网uu
~mF8RJ
try{51Testing软件测试网0_5i$s4wg
conn=DriverManager.getConnection(DatabaseConnStr,userName,password);51Testing软件测试网bsf UY&F@
Statement stmt=conn.createStatement( );
[&{d'VH4c2|#m+@0stmt.executeUpdate(sql);
a4mx f;T,Sl5JWw0}51Testing软件测试网 DqlO)w|
catch(SQLException ex){
eT'W
NF;Q2\0System.err.println("执行插入有错误:"+ex.getMessage() );
l'm7_Nd4~`0System.out.print("执行插入有错误:"+ex.getMessage());//输出到客户端51Testing软件测试网(X] D&dM
}51Testing软件测试网K.p|eEE
}51Testing软件测试网)Oe_s i
// display data
c3W7LLr0public ResultSet executeQuery(String sql){
4jzS7]3W0rs=null;51Testing软件测试网6[9I6qhH.Zd[
try{51Testing软件测试网O*R^9R%L5ueK(W
conn=DriverManager.getConnection(DatabaseConnStr,userName,password);51Testing软件测试网j
V#I*T)Ybx5R;J-U2lB!{
Statement stmt=conn.createStatement( );51Testing软件测试网u;g{/m8M~+A6a"U1E
rs=stmt.executeQuery(sql);51Testing软件测试网5glbgWu3b*pq,m
}
c+BSw%h[W0catch(SQLException ex){
U1|/h;F q9DOO)S!L0System.err.println("执行查询有错误:"+ex.getMessage() );51Testing软件测试网Zfm#W#a3fz0@AA
System.out.print("执行查询有错误:"+ex.getMessage()); //输出到客户端51Testing软件测试网e~2JI8e6pq H
}51Testing软件测试网0Us&y OU
return rs;
@.i?*])a;^BF0}
*FM)@"xjR,X0// delete data
)w)d^7[2s)O_Tin0public void executeDelete(String sql){
`Y6Z*H^:m0try{
f]okCGi$]!]0conn=DriverManager.getConnection(DatabaseConnStr,userName,password);
3I}+bo"}e0Statement stmt=conn.createStatement( );51Testing软件测试网+O'Z$v|q*`"kX
o5gN
stmt.executeUpdate(sql);51Testing软件测试网g!W?;U:k E6\T D
}
a:Wy ~XJ?'I:c0catch(SQLException ex){51Testing软件测试网\{z]c
System.err.println("执行删除有错误:"+ex.getMessage() );
_ E+[t"N/]A-N1p,Q0System.out.print("执行删除有错误:"+ex.getMessage()); //输出到客户端
%B"^I+KjfM0}
nj(B^F.m%q0}
+Aodt?bq3B;Z0//51Testing软件测试网 J.TBWluz6O
public void CloseDataBase(){51Testing软件测试网Ni)Y"]Z8\"j~j
try{
Z(o1sGI0conn.close();
@'x%bJ7u-t6C[ L\ iR%e0}51Testing软件测试网]5HejAz
catch(Exception end){
6pa)?#spo|
N"ao0System.err.println("执行关闭Connection对象有错误:"+end.getMessage( ) );51Testing软件测试网Wupff)T*n
System.out.print("执行执行关闭Connection对象有错误:有错误:"+end.getMessage()); //输出到客户端51Testing软件测试网[E5ys
Yai
}
vq#A&O+C^S
U.o0}
6B2Yk:|:A*J+NP0}