简体   繁体   中英

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. Personally I'm a huge fan of using Spring regardless of which Web framework you choose. It just makes so many things that much easier. Lightweight persistence frameworks include Spring JDBC and, my favourite, Ibatis .

In fact I wrote a tutorial on using Spring and Ibatis . In fact, it even uses 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.

  • don't retrieve data in JSP, use an MVC architecture or at least retrieve the data in the servlet
  • use Spring
  • write some DAO classes or if you prefer am ORM use iBatis or 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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