fengling981825的个人空间 https://blog.eetop.cn/fengling [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

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

已有 2279 次阅读| 2006-8-10 05:47 |个人分类:编程

天气: 晴朗
心情: 高兴

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);
// }
}
 


点赞

发表评论 评论 (1 个评论)

Guest 2007-4-25 22:55
有 精通开发SIM卡的吗?请联系我13552511726,高报酬 ,电子邮件FENLONG.1968@163.COM

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 1

    粉丝
  • 0

    好友
  • 1

    获赞
  • 51

    评论
  • 658

    访问数
关闭

站长推荐 上一条 /1 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-3-28 19:43 , Processed in 0.014054 second(s), 8 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部