zoukankan      html  css  js  c++  java
  • jndi与rmi传递数据创建工作薄

    通过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(); } } }

     
  • 相关阅读:
    Java小白集合源码的学习系列:Vector
    Java小白集合源码的学习系列:LinkedList
    707.设计双向链表
    Java小白集合源码的学习系列:ArrayList
    Leetcode动态规划【简单题】
    小白学Java:老师!泛型我懂了!
    小白学Java:包装类
    Java面向对象之异常详解
    Java面向对象之异常【一】
    浅谈Java中接口与抽象类的异同
  • 原文地址:https://www.cnblogs.com/b422/p/jndi_rmi.html
Copyright © 2011-2022 走看看