当前位置导航:炫浪网>>网络学院>>编程开发>>Oracle教程

oracle数据库java接口类


  /**************2004-04-21**************from wj*/
  
  /*****oracle数据库接口***********/
  
  package CMCC;
  
  import java.sql.* ;
  
  import java.util.*;
  
  import java.text.*;
  
  import java.io.*;
  
  import oracle.jdbc.driver.* ;
  
  public class jdbc_oracle_test
  
  {
  
  /*
  
  public static void main ( String args[]){
  
  jdbc_oracle db = new jdbc_oracle();
  
  String Str;
  
  int id;
  
  ResultSet re = null ;
  
  re = db.query ("select * from test");
  
  try{
  
  int i=0 ;
  
  while (re.next () && i<10){
  
  Str = re.getString("id");
  
  System.out.println(Str );
  
  i++ ;
  
  }
  
  }
  
  catch (Exception e ){
  
  System.out.println("error!" );
  
  }
  
  db.closestmt();
  
  db.closeconn();
  
  }
  
  //*/
  
  //String conn_str="jdbc :microsoft :sqlserver://127.0.0.1 ;DatabaseName=test"
  ;//——数据库
  
  String conn_str="jdbc :oracle:thin:@127.0.0.1:1521:test" ;
  
  String username="test";//——数据库用户名
  
  String password="test";//——数据库密码
  
  private Connection conn=null;
  
  private Statement stmt=null ;
  
  ResultSet rs=null ;
  
  //——数据库链接——
  
  public jdbc_oracle_test ()
  
  {
  
  try
  
  {
  
  Class.forName ("oracle.jdbc.driver.OracleDriver" );
  
  }
  
  catch (java.lang.ClassNotFoundException e) {System.err.println
  ("dbclass():"+e.getMessage());}
  
  try
  
  {
  
  conn=DriverManager.getConnection(conn_str,username,password);
  
  }
  
  catch (SQLException ex )
  
  { System.err.println("dbclass():"+ex.getMessage ());}
  
  }
  
  //——取得数据库连接——
  
  public Connection getConn ()
  
  {
  
  return conn ;
  
  }
  
  //——数据库查询——public ResultSet query(String sql)
  
  {
  
  try
  
  {
  
  stmt=conn.createStatement ();
  
  rs=stmt.executeQuery(sql );}
  
  catch (SQLException ex ) {System.err.println("query():"+ex.getMessage
  ());}
  
  return rs ;
  
  }
  
  //——————————-数据库修改————————-
  
  public void update(String sql)
  
  {
  
  try
  
  {
  
  stmt=conn.createStatement ();
  
  stmt.executeUpdate(sql );}
  
  catch (SQLException ex ) {System.err.println("update ():"+ex.getMessage
  ());}
  
  }
  
  //——————————关闭数据源——————-
  
  public void closestmt ()
  
  {
  
  try{stmt.close();}
  
  catch (SQLException ex ) {System.err.println("closestmt():"+ex.getMessage
  ());}
  
  }
  
  //——————————关闭链接————————-
  
  public void closeconn ()
  
  {
  
  try{conn.close();}
  
  catch (SQLException ex ) {System.err.println("closeconn()"+ex.getMessage
  ());}
  
  }
  
  //——转换中文
  
  public String toChinese (String s)
  
  {
  
  if(s==null )
  
  return null ;
  
  try
  
  {
  
  String convert=new String (s.getBytes("ISO8859-1" ),"gb2312")
  ;
  
  return convert;
  
  }
  
  catch (Exception e ){}
  
  return null ;
  
  }
  
  //——分页
  
  public String page1 (int num ,int line,int page_line ,int start
  ,int page ,String search ,String filename )
  
  { int pagetotal=line*page_line;//所要显示的总条数
  
  int pagestart ;// int pagenum;//
  
  int total=(num-start+line-1)/line ;//取得总页数
  
  int total_start=(num+pagetotal-1 )/pagetotal;//取得总偏移数
  
  String returnstr="" ;
  
  if(start !=0)
  
  {
  
  int old_start=start-pagetotal ;
  
  returnstr+="  ;  ;<a href="+filename+"?page=0&start="+old_start+search+"><<</a>"
  ;
  
  }
  
  pagestart=start/pagetotal*page_line ;//取得当前页数
  
  for (int i=0 ;i<total&&i<page_line;i++ )
  
  {
  
  pagenum=pagestart+i+1 ;
  
  returnstr+="  ;  ;<a href="+filename+"?page="+i+"&start="+start+search+">"+pagenum+"</a>"
  ;
  
  }
  
  if(total_start != (start/pagetotal+1 )&&num !=0)
  
  {
  
  int new_start=start+pagetotal ;
  
  returnstr+="  ;  ;<a href="+filename+"?page=0&start="+new_start+search+">>></a>"
  ;
  
  }
  
  return returnstr;
  
  }
  
  //——取得时间
  
  public String GetPath ()
  
  {
  
  String dateString="123" ;
  
  try
  
  {
  
  java.text.SimpleDateFormat formatter=new java.text.SimpleDateFormat
  ("yyyy-MM" );
  
  java.util.Date currentTime_1=new java.util.Date ();
  
  dateString=formatter.format (currentTime_1 );
  
  }
  
  catch (Exception e )
  
  {
  
  }
  
  return dateString ;
  
  }
  
  //——取得时间
  
  public String strNowtime()
  
  {
  
  String dateString="123" ;
  
  try
  
  {
  
  java.text.SimpleDateFormat formatter=new java.text.SimpleDateFormat
  ("yyyy-MM-dd hh:mm:ss" );
  
  java.util.Date currentTime_1=new java.util.Date ();
  
  dateString=formatter.format (currentTime_1 );
  
  }
  
  catch (Exception e )
  
  {
  
  }
  
  return dateString ;
  
  }
  
  //——去掉html——
  
  public String HTMLEncode(String Str)
  
  {
  
  Str=ebbReplace(Str ,"<" ,"<;" );
  
  Str=ebbReplace(Str ,">" ,">;" );
  
  Str=ebbReplace(Str ,"\n","<BR>");
  
  return Str;
  
  }
  
  //——替换变量——
  
  public String ebbReplace(String Str,String oldStr ,String newStr)
  
  {
  
  String ReturnStr="" ;
  
  int i ,j ,t ,m ,n ;
  
  n=0 ;
  
  j=oldStr.length ();
  
  if(Str.indexOf (oldStr)>-1 )
  
  {
  
  while (Str.indexOf (oldStr,n )>-1 )
  
  {
  
  i=Str.length();
  
  if(Str.indexOf (oldStr)==0 )
  
  Str=newStr+Str.substring(j ,i );
  
  else
  
  {
  
  t=Str.indexOf (oldStr);
  
  m=(t+j );
  
  Str=Str.substring (0 ,t )+newStr+Str.substring (m ,i );
  
  n=t+newStr.length ()-j+1;
  
  }
  
  }
  
  }
  
  ReturnStr=Str ;
  
  return ReturnStr;
  
  }
  
  //——ubb 替换——
  
  public String LCReplace (String Str,String BStr ,String EStr ,
  String ReStr)
  
  {
  
  String ReturnStr="" ,Str1=""
相关内容
赞助商链接