import java.sql.*;
import java.io.*;
import javax.naming.*;
import javax.sql.*;
import java.util.*;
public class dbaction{
public connection conn = null;
public preparedstatement stmt=null;
public properties getproperties(string str){
properties properties = new properties();
try{
inputstream is =getclass().getresourceasstream("/"+str);
properties.load(is);
if(is != null)
is.close();
}
catch(ioexception ioexception){
system.out.println("open config file failure.");
}
catch(nullpointerexception e){
system.out.println("is is null");
}
return properties;
}
public synchronized void dbconnect(){
string strcon=null;
properties properties = getproperties("datasource.properties");
string username = properties.getproperty("username");
string password = properties.getproperty("password");
string hostname = properties.getproperty("hostname");
string hostip = properties.getproperty("hostip");
string hostport = properties.getproperty("hostport");
try
{
class.forname("oracle.jdbc.driver.oracledriver");
}
catch(classnotfoundexception classnotfoundexception)
{
system.out.println("could not load the driver.");
classnotfoundexception.printstacktrace();
}
strcon = "jdbc:oracle:thin:@"+hostip+":"+hostport+":"+hostname;
try
{
conn = drivermanager.getconnection(strcon,username,password);
}
catch(sqlexception sqlexception)
{
system.out.println("creat connection error.");
sqlexception.printstacktrace();
}
// try{
// context initcontext = new initialcontext(); //连接池用
// context envcontext = (context) initcontext.lookup("java:/comp/env");
// datasource ds = (datasource) envcontext.lookup("jdbc/oracle");
// conn = ds.getconnection();
// }
// catch(namingexception ne){
// ne.printstacktrace();
// }
// catch(sqlexception se){
// se.printstacktrace();
// }
}
public string sqlsearch(string str){
string sql=null;
properties properties = getproperties("sql.properties");
sql = properties.getproperty(str);
return sql;
}
public resultset dbselect(string sql){
resultset rs=null;
try{
this.stmt = conn.preparestatement(sql.trim());
rs = this.stmt.executequery();
}
catch(sqlexception e){
e.printstacktrace();
}
return rs;
}
public resultset dbselect(string sql,string str1){
resultset rs=null;
try{
this.stmt = conn.preparestatement(sql.trim());
this.stmt.setstring(1,str1);
rs = this.stmt.executequery();
}
catch(sqlexception e){
e.printstacktrace();
}
return rs;
}
public resultset dbselect(string sql,string str1,string str2){
resultset rs=null;
try{
this.stmt = conn.preparestatement(sql.trim());
this.stmt.setstring(1,str1);
this.stmt.setstring(2,str2);
rs = this.stmt.executequery();
}
catch(sqlexception e){
e.printstacktrace();
}
return rs;
}
public int dbupd(string sql){
int rtncd=0;
try{
stmt = conn.preparestatement(sql);
rtncd = stmt.executeupdate();
if(rtncd != 0)rtncd = 1;
}
catch(exception e){
e.printstacktrace();
}
return rtncd;
}
public int dbdel(string sql){
int rtncd=0;
try{
stmt = conn.preparestatement(sql);
rtncd = stmt.executeupdate();
if(rtncd != 0)rtncd = 1;
}
catch(exception e){
e.printstacktrace();
}
return rtncd;
}
public int dbadd(string sql){
int rtncd=0;
try{
stmt = conn.preparestatement(sql);
rtncd = stmt.executeupdate();
if(rtncd != 0)rtncd = 1;
}
catch(exception e){
e.printstacktrace();
}
return rtncd;
}
public void close(){
try{
if(stmt!=null)stmt.close();
if(conn!=null)conn.close();
}
catch(sqlexception e){
e.printstacktrace();
}
}
public string replasbstr(string strsource,string strfrom,string strto){
if (strsource == null) {
return null; }
int i = 0;
if ((i = strsource.indexof(strfrom, i)) >= 0) {
char[] csrc = strsource.tochararray();
char[] cto = strto.tochararray();
int len = strfrom.length();
stringbuffer buf = new stringbuffer(csrc.length);
buf.append(csrc,0,i);
buf.append(cto);
//buf.append(csrc, 0, i).append(cto);
i += len;
int j = 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);
return buf.tostring(); }
return strsource;
}
// public static void main(string[] args) throws exception{
// dbaction d = new dbaction();
// if (d.conn!=null)system.out.println("success");
// string sql = d.sqlsearch("codesqlsle");
// system.out.println(sql);
// //resultset rs = d.dbselect(sql);
// //if (rs!=null)system.out.println("success");
//// try{
//// statement stmt=conn.createstatement();
//// string strsql = "select codeid from js_code_tbl";
//// resultset rs = stmt.executequery(strsql);
//// if(rs==null)system.out.println("creat rs error");
//// while(rs.next()){
//// string e=rs.getstring("codeid");
//// system.out.println(e);
//// }
//// rs.close();
//// stmt.close();
//// }
//// catch(sqlexception e){
//// system.out.println("error");
//// }
// string i = "-123456789012.311111111111111111111";
// string j = moneychk(i);
// system.out.println(j);
// }
}
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




