随笔-28  评论-15  文章-81  trackbacks-0

///下面为连接Oracle数据库的代码
package com.tan;

import jxl.write.WritableWorkbook;
import jxl.write.WritableSheet;
import jxl.write.Label;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import jxl.Workbook;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestExcel {
 public static void main(String[] args) throws InstantiationException,
   IllegalAccessException, ClassNotFoundException, IOException,
   RowsExceededException, WriteException {
  Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
  String url = "jdbc:oracle:thin:@oracledbserver:1521:version7";
  Connection conn;

  try {
   conn = DriverManager.getConnection(url, "dbown", "dbown");
   Statement stmt = conn
     .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
       ResultSet.CONCUR_UPDATABLE);
   String sql = "select ordseq,ordno from ord where ordno is not null";
   ResultSet rs = stmt.executeQuery(sql);
   WritableWorkbook book = Workbook
     .createWorkbook(new File("c:/test.xls"));
   WritableSheet sheet = book.createSheet("第一页", 0);      
   int i = 0;
   while (rs.next()) {

    //System.out.println(rs.getString(1));
    //System.out.println(rs.getString(2));
    Label label = new Label(0, i, rs.getString(1));
    Label label2 = new Label(1, i, rs.getString(2));
    sheet.addCell(label);
    sheet.addCell(label2);
    String name = rs.getString("ordno");
    if (name.equals("SO024"))
    {
     System.out.println(rs.getString("ordno"));
    }
    i = i + 1;
   }

   System.out.println("数据库操作成功,恭喜你");
   System.out.println("总共有: " + i + "行");
   book.write();
   book.close();
   rs.close();
   stmt.close();
   conn.close();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   System.out.println(e.getMessage());
  }

 }

}

/********************************************/

上面的name如果为空则会报空指针异常的错:
      java.lang.NullPointerException  
  原因由 ''.equals("name");  引起
 

posted on 2008-12-02 15:37 谭明 阅读(196) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: