一个联接Oracle数据库的Java程序

使用Java语言编制JDBC程序联接Oracle数据库-------------- [@more@]

使用JBuilder 10.0.1 Foundtion Edtion编写,代码如下:

 package accessdatabase;

/**
 *

Title:


 *

Description:


 *

Copyright: Copyright (c) 2004


 *

Company:


 * @author not attributable
 * @version 1.0
 */
/**
 *this class using to get the connection with the used database for other class
 *used to get the select or insert or update result from the resultset.
 */

import javax.sql.*;
import java.sql.*;
import java.net.URL;
public class ConnSql {
  public ConnSql(){}
  private static boolean checkForWarning(SQLWarning warn)
      throws SQLException
  {
    boolean rc= false;
     //if SQl Warning object is not null,its means has catch the exception.
     //display the relation information.
     if(warn!=null){
       System.out.println("n***Warning***n");
       rc=true;
       while(warn!=null){
           System.out.println("SQLState:"+warn.getSQLState());
           System.out.println("Message:"+warn.getMessage());
           System.out.println("Vendor:"+warn.getErrorCode());
           System.out.println("");
           warn=warn.getNextWarning();
       }
     }
     return rc;
  }
  //===========================
  //dispResultSet
  //============================

  private static void dispResultSet(ResultSet rs)
      throws SQLException
  {
    int i;
    //get the resultSet of medata.
    ResultSetMetaData rsmd=rs.getMetaData();
    //catch the resultSet's hash column.
    int numCols = rsmd.getColumnCount();
    //display title.
    for(i=1;i<=numCols;i++){
        if(i>1)System.out.print(",");
        System.out.println(rsmd.getColumnLabel(i));
    }
   System.out.println("");
   //display all data in the resultSet.
   boolean more = rs.next();
   while(more){
       //do for while to display the all data in the resultSet.
       for(i=1;i<=numCols;i++){
           if(i>1)System.out.print(",");
           System.out.print(rs.getString(i));
       }
       System.out.println("");
       //catch the next row data.
       more=rs.next();
    }
  }
  public static void main(String args[]) {

    String sUsr="xxg";
    String sPwd="xxg";
    String query="select * from xxg";
    try{
         //add jdbc-odbc briger driver.
         Class.forName("oracle.jdbc.driver.OracleDriver");

  //set a note streamer for noted the execute procedure for artlog the JDBC.
        DriverManager.setLogStream(System.out);
  //conect to the Driver program.the Driver which system has registered,
  //will be connected and added.
  //end when get a right Driver.
  Connection con =
   DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xxg",sUsr,sPwd);
 
//when go here is mean connection is sccessful.

  //check and display the artlen and error message.
     checkForWarning(con.getWarnings());
  //create a Statement object,using its to set SQL query to the Driver.
    Statement stmt = con.createStatement();
  //taken the SQL query,create a ResultSet object.
  ResultSet rs = stmt.executeQuery(query);
  //display the resultset for all roe and column.
  dispResultSet(rs);
  //close the ResultSet.
   rs.close();
   //close statement object.
   stmt.close();
  
//close connection.

    }catch(SQLException ex) {
      //catch error message,display them.
      System.out.println("n***SQLException caught**n");
      while(ex!=null) {
           System.out.println("SQLState:"+ ex.getSQLState());
           System.out.println("Message:"+ ex.getMessage());
           System.out.println("Vendor:"+ ex.getErrorCode());
           ex=ex.getNextException();
           System.out.println("");

      }
    }catch(java.lang.Exception ex) {
       //catch other class's exception.
       ex.printStackTrace();
    }
 
}

}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/20546452/viewspace-785144/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/20546452/viewspace-785144/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值