Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1997022
  • 博文数量: 1647
  • 博客积分: 80000
  • 博客等级: 元帅
  • 技术积分: 9980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 15:15
文章分类

全部博文(1647)

文章存档

2011年(1)

2008年(1646)

我的朋友

分类:

2008-10-28 17:59:15


  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.Driver");
  }
  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);
  // }
  }
【责编:admin】

--------------------next---------------------

阅读(231) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~