• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

servlet/jsp 搞代码 7年前 (2018-06-18) 153次浏览 已收录 0个评论

package common;
import java.sql.*;
import java.io.*;
import javax.naming.*;
import javax.sql.*;
import java.util.*;

http://www.gaodaima.com/41260.htmljava连接oracle,取io文件内容,子串替

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

欢迎大家阅读《java连接oracle,取io文件内容,子串替》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:java连接oracle,取io文件内容,子串替
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址