简体   繁体   English

如何在Java Servlet和JSP中从10g检索数据

[英]How to retrieve data from 10g in java servlet & JSP

在Servlet和JSP中从数据库10g检索数据

There are countless ways of skinning this particular cat. 有很多方法可以给这只猫剥皮。

For one it depends on what Web framework (if any) you use. 一个取决于您使用的Web框架(如果有)。 Personally I'm a huge fan of using Spring regardless of which Web framework you choose. 就个人而言,无论您选择哪种Web框架,我都是使用Spring的忠实粉丝。 It just makes so many things that much easier. 它使很多事情变得容易得多。 Lightweight persistence frameworks include Spring JDBC and, my favourite, Ibatis . 轻量级的持久性框架包括Spring JDBC和我最喜欢的Ibatis

In fact I wrote a tutorial on using Spring and Ibatis . 实际上,我编写了有关使用Spring和Ibatis教程 In fact, it even uses Oracle 10g Express Edition ("Oracle XE"). 实际上,它甚至使用Oracle 10g Express Edition (“ Oracle XE”)。

我假设你的意思是Oracle 10g数据库,如果有JDBC为答案,就在这里 (普通)和这里 (Oracle JDBC驱动程序)。

Use (the ordering is my preference) 使用(订购是我的偏爱)

Don't use direct JDBC unless you just have a bunch of extra time. 除非您只有一堆额外的时间,否则请不要使用直接JDBC。

  • don't retrieve data in JSP, use an MVC architecture or at least retrieve the data in the servlet 不要在JSP中检索数据,使用MVC架构或至少检索servlet中的数据
  • use Spring 使用Spring
  • write some DAO classes or if you prefer am ORM use iBatis or Hibernate 编写一些DAO类,或者如果您更喜欢ORM,请使用iBatis或Hibernate
  • refine your question if you need more specific information, as it is it's a bit vague regarding what exactly you need to know 如果您需要更具体的信息,请完善您的问题,因为对于您确切需要了解的内容而言,这有点含糊

Other answers have listed the best technologies that one should definitely pursue to accomplish this. 其他答案也列出了为实现这一目标而应该追求的最佳技术。 But to directly answer the question, perhaps the most straight forward answer is with an example of plain old JDBC: 但是要直接回答这个问题,也许最直接的答案是一个简单的旧JDBC示例:

private void getYourData() {
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rset = null;
    try {
        Context initContext = new InitialContext();
        Context envContext = (Context) initContext.lookup("java:/comp/env");
        DataSource ds = (DataSource) envContext.lookup("jdbc/yourDatabase");
        conn = ds.getConnection();
        pstmt = conn.prepareStatement(
                "select yourdata " +
                "  from yourtable " +
                "  where yourkey = ? "
                );

        pstmt.setInt(1, yourKeyValue);
        rset = pstmt.executeQuery();
        while (rset.next()) {
            String yourData = rset.getString("yourdata");
        }

        conn.commit();

    } catch (NamingException ne) {
        log.error(ne.getMessage());
    } catch (SQLException se) {
        log.error(se.getMessage());
    } finally {
        if (rset != null) {
            try {
                rset.close();
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM