最基础的JDBC连接SYBASE方式

 

package com.citi.hnw.mainframe;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class getCusipList {
 private static Logger logger = Logger.getLogger(getCusipList.class);
 
 public static void main(String[] args) throws SQLException{
  getCusipList cusip = new getCusipList();
  cusip.getList();
 }
 
 public void getList() throws SQLException{
  Connection con = null;
  try{
   con = getConnection();
  }finally{
   closeConnection(con);
  }
 }
   
 public Connection getConnection(){
  Connection con = null;
  Statement stmt = null;
  ResultSet rs = null;
  List<String> list = new ArrayList();
  String sql="select distinct Cusip as cusip from HnwFeedPosition";
  try{
   Class.forName("com.sybase.jdbc2.jdbc.SybDriver");
   String connectionUrl="jdbc:sybase:Tds:dbname:port?ServiceName=dbname";
   con = DriverManager.getConnection(connectionUrl,"username","password");
   System.out.println("Connection detail is : "
     + con.getMetaData().getDatabaseProductName()
     + "," + con.getMetaData().getURL() 
     + "," + con.getMetaData().getUserName());
   stmt = con.createStatement();
   stmt.setFetchSize(500);
   rs = stmt.executeQuery(sql);
   while(rs.next()){
    list.add(rs.getString("cusip"));
    System.out.println(list.size());
   }
  }catch(Exception ex){
   ex.printStackTrace();
  }
  return con;
 }
 
 public void closeConnection(Connection con){
  if( null != con ){
   try {
    con.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }
 
 public void closeStatement(Statement stmt){
  if( null != stmt ){
   try {
    stmt.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }
 public void closeResultSet(ResultSet rs){
  if( null != rs ){
   try {
    rs.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }
}

 

posted @ 2013-06-12 19:36  爱生活,爱编程  阅读(891)  评论(0编辑  收藏  举报