·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » java连接oracle,取io文件内容,子串替换

java连接oracle,取io文件内容,子串替换

类别: JAVA教程  评论数:0 总得分:0
package common;
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);
// }
}






-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1