通过JNDI Api 的方式访问数据源,在tomcat 下的conf/context.xml中配置如下代码:

<Resource name="sqlconn" 
        auth="Container" 
        type="javax.sql.DataSource"
        driverClassName="oracle.jdbc.driver.OracleDriver" //连接oracle的驱动
        url="jdbc:oracle:thin:@localhost:1521:orcl"  //连接oracle的地址
        username="scott"                
        password="tiger"                 
        maxActive="100" 
        maxIdle="10" 
        maxWait="10000" />

在java web工程中获得数据源对象代码如下:

public static Connection getconn(){
        Connection conn = null;
        
        try {
            Context ctx= new InitialContext();
            DataSource ds = (DataSource) ctx.lookup("java:comp/env/sqlconn");
            conn = ds.getConnection();
            System.out.println("数据连接池开启");
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return conn;
    }

返回通过connection查询的数据集合:

public List<Emp> find(){
        Connection conn = JndlTest.getconn();
        List<Emp> emps = new ArrayList<Emp>();
        
        try {
            PreparedStatement ps = conn.prepareStatement("select * from emp");
            ResultSet rs =ps.executeQuery();
            
            while (rs.next()) {
                Emp emp = new Emp();
                emp.setEmpno(rs.getInt("empno"));
                emp.setEname(rs.getString("ename"));
                emp.setSal(rs.getInt("sal"));
                emps.add(emp);
            }
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return emps;
    }

开启rmi访问端口:

Registry r = LocateRegistry.createRegistry(2222);//开启端口
        RmiDao rmi = new RmiDaoImle();  //自定义用来访问数据库数据的javabean
        try {
            r.bind("emp",rmi);      //把接口传递过去
            out.print("开启成功");
        } catch (AlreadyBoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

在一个java工程中接受同时生成excle文件同时需要导入apache的poi包:

public class Test {
    
    public static String outputFile="D:/gongye.xls";

    
    public static void main(String[] args) {
        try {
            //获得web工程的链接
            RmiDao rmi = (RmiDao)Naming.lookup("rmi://localhost:2222/emp");
            List<Emp>
 emps = rmi.find();
            // 创建新的Excel 工作簿
            HSSFWorkbook hssf = new HSSFWorkbook();
            
            HSSFSheet sheet = hssf.createSheet();    
            int index = 0;
       //遍历添加到excel表格中
for(Emp emp:emps){ HSSFRow row = sheet.createRow(index); HSSFCell cell = row.createCell(0); HSSFCell cell2 = row.createCell(1); HSSFCell cell3 = row.createCell(2); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(emp.getEmpno()); cell2.setCellValue(emp.getEname()); cell3.setCellValue(emp.getSal()); index++; System.out.println(emp.getEname()); } FileOutputStream stream = new FileOutputStream(outputFile); hssf.write(stream); stream.flush(); stream.close(); System.out.println("文件生成!"); } catch (MalformedURLException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } catch (NotBoundException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

 
posted on 2012-05-28 09:16  断肠夕阳丿  阅读(354)  评论(0编辑  收藏  举报