|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
Java伴随着互联网的迅猛发展而发展,逐渐成为重要的网络编程语言。Oracle收购Sun后Java前途未卜。oraclepackagecommon;
importjava.sql.*;
importjava.io.*;
importjavax.naming.*;
importjavax.sql.*;
importjava.util.*;
publicclassDbAction{
publicConnectionconn=null;
publicPreparedStatementstmt=null;
publicPropertiesgetProperties(Stringstr){
Propertiesproperties=newProperties();
try{
InputStreamis=getClass().getResourceAsStream("/"+str);
properties.load(is);
if(is!=null)
is.close();
}
catch(IOExceptionioexception){
System.out.println("Openconfigfilefailure.");
}
catch(NullPointerExceptione){
System.out.println("isisnull");
}
returnproperties;
}
publicsynchronizedvoidDbConnect(){
StringstrCon=null;
Propertiesproperties=getProperties("datasource.properties");
Stringusername=properties.getProperty("username");
Stringpassword=properties.getProperty("password");
Stringhostname=properties.getProperty("hostname");
Stringhostip=properties.getProperty("hostip");
Stringhostport=properties.getProperty("hostport");
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundExceptionclassnotfoundexception)
{
System.out.println("Couldnotloadthedriver.");
classnotfoundexception.printStackTrace();
}
strCon="jdbc:oracle:thin:@"+hostip+":"+hostport+":"+hostname;
try
{
conn=DriverManager.getConnection(strCon,username,password);
}
catch(SQLExceptionsqlexception)
{
System.out.println("Creatconnectionerror.");
sqlexception.printStackTrace();
}
//try{
//ContextinitContext=newInitialContext();//毗连池用
//ContextenvContext=(Context)initContext.lookup("java:/comp/env");
//DataSourceds=(DataSource)envContext.lookup("jdbc/oracle");
//conn=ds.getConnection();
//}
//catch(NamingExceptionne){
//ne.printStackTrace();
//}
//catch(SQLExceptionse){
//se.printStackTrace();
//}
}
publicStringsqlSearch(Stringstr){
Stringsql=null;
Propertiesproperties=getProperties("sql.properties");
sql=properties.getProperty(str);
returnsql;
}
publicResultSetdbSelect(Stringsql){
ResultSetrs=null;
try{
this.stmt=conn.prepareStatement(sql.trim());
rs=this.stmt.executeQuery();
}
catch(SQLExceptione){
e.printStackTrace();
}
returnrs;
}
publicResultSetdbSelect(Stringsql,Stringstr1){
ResultSetrs=null;
try{
this.stmt=conn.prepareStatement(sql.trim());
this.stmt.setString(1,str1);
rs=this.stmt.executeQuery();
}
catch(SQLExceptione){
e.printStackTrace();
}
returnrs;
}
publicResultSetdbSelect(Stringsql,Stringstr1,Stringstr2){
ResultSetrs=null;
try{
this.stmt=conn.prepareStatement(sql.trim());
this.stmt.setString(1,str1);
this.stmt.setString(2,str2);
rs=this.stmt.executeQuery();
}
catch(SQLExceptione){
e.printStackTrace();
}
returnrs;
}
publicintdbUpd(Stringsql){
intrtncd=0;
try{
stmt=conn.prepareStatement(sql);
rtncd=stmt.executeUpdate();
if(rtncd!=0)rtncd=1;
}
catch(Exceptione){
e.printStackTrace();
}
returnrtncd;
}
publicintdbDel(Stringsql){
intrtncd=0;
try{
stmt=conn.prepareStatement(sql);
rtncd=stmt.executeUpdate();
if(rtncd!=0)rtncd=1;
}
catch(Exceptione){
e.printStackTrace();
}
returnrtncd;
}
publicintdbAdd(Stringsql){
intrtncd=0;
try{
stmt=conn.prepareStatement(sql);
rtncd=stmt.executeUpdate();
if(rtncd!=0)rtncd=1;
}
catch(Exceptione){
e.printStackTrace();
}
returnrtncd;
}
publicvoidclose(){
try{
if(stmt!=null)stmt.close();
if(conn!=null)conn.close();
}
catch(SQLExceptione){
e.printStackTrace();
}
}
publicStringreplaSbstr(StringstrSource,StringstrFrom,StringstrTo){
if(strSource==null){
returnnull;}
inti=0;
if((i=strSource.indexOf(strFrom,i))>=0){
char[]cSrc=strSource.toCharArray();
char[]cTo=strTo.toCharArray();
intlen=strFrom.length();
StringBufferbuf=newStringBuffer(cSrc.length);
buf.append(cSrc,0,i);
buf.append(cTo);
//buf.append(cSrc,0,i).append(cTo);
i+=len;
intj=i;
while((i=strSource.indexOf(strFrom,i))>0){
buf.append(cSrc,j,i-j);
buf.append(cTo);
//buf.append(cSrc,j,i-j).append(cTo);
i+=len;
j=i;
}
buf.append(cSrc,j,cSrc.length-j);
returnbuf.toString();}
returnstrSource;
}
//publicstaticvoidmain(String[]args)throwsException{
//DbActiond=newDbAction();
//if(d.conn!=null)System.out.println("success");
//Stringsql=d.sqlSearch("codesqlsle");
//System.out.println(sql);
////ResultSetrs=d.dbSelect(sql);
////if(rs!=null)System.out.println("success");
////try{
////Statementstmt=conn.createStatement();
////StringstrSQL="SELECTCODEIDFROMJS_CODE_TBL";
////ResultSetrs=stmt.executeQuery(strSQL);
////if(rs==null)System.out.println("creatrserror");
////while(rs.next()){
////Stringe=rs.getString("CODEID");
////System.out.println(e);
////}
////rs.close();
////stmt.close();
////}
////catch(SQLExceptione){
////System.out.println("error");
////}
//Stringi="-123456789012.311111111111111111111";
//Stringj=moneychk(i);
//System.out.println(j);
//}
}
net程序员的大部门代码都靠控件拖拽完成的,虽然java也有,但是无论从美观和速度上都没发和.net比。java程序员都是代码完成的,所以java程序员常戏称.net程序员是操作员,呵呵。 |
|