• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

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

发布: 2007-7-04 13:34 | 作者: admin | 来源:  网友评论 | 查看: 16次 | 进入软件测试论坛讨论

领测软件测试网 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);
// }
}



延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网