⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbcreadservlet.java

📁 一个完整的
💻 JAVA
字号:
//package projects.toptais;

import java.io.*;
import java.sql.*;
import javax.servlet.http.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class JDBCReadServlet extends HttpServlet {

  
  /*
   * This method is called when the servlet is first initialized.
   * Note here that both the initial lookup into the WebLogic JNDI
   * naming context and the location of a DataSource object from it
   * are placed here.  This ensures that the operations are only done
   * once instead of every time that the servlet is accessed.  This is
   * very important for efficiency.
   */
  public void init() {

    try  {

      /* Create a connection to the WebLogic JNDI Naming Service:
       */
      ctx = new InitialContext();

      /*  Create a new DataSource by Locating It in the Naming Service:
          OracleThinPool is the name of the connection pool name configed in the WebLogic server container.
          WebLogic data connection pool configration:
          {
          	URL:                     jdbc:oracle:thin:@pyds:1521:toptais  //"toptais" is database name in our RDBMS. "pyds" is DB server name.
            Driver Classname:        oracle.jdbc.driver.OracleDriver
            Properties (key=value):  user=scott
                                     password=tiger
                                     dll=ocijdbc8
                                     protocol=thin
          }
       */
      ds = (javax.sql.DataSource) 
        ctx.lookup ("OracleThinPool");

    } catch (Exception E) {
      /* 
         Handle exception here. 
      */
      System.out.println("Init Error: " + E);
    }
  }


  public void service(HttpServletRequest requ, 
    HttpServletResponse resp) 
    throws IOException

  {
    Connection myConn = null; 

    try  {

      PrintWriter out = resp.getWriter();

      out.println("<html>");
      out.println("<head><title>_JDBCReadServlet</title></head>");
      out.println("<body>");

      out.println("<h1>_JDBCReadServlet</h1>");      

      /* Get a new JDBC connection from the DataSource:
       */
      myConn = ds.getConnection();

      /* Create an Instance of the java.sql.Statement class 
         and use the factory method called createStatement() 
         available in the Connection class to create a new statement. 
      */
      stmt = myConn.createStatement();

      /* Use the shortcut method the available in the Statement 
         class to execute our query.  We are selecting all rows 
         from the EMPLOYEE table. 
      */
      rs = stmt.executeQuery("SELECT FSTRTABLENAME,FSTRCLASS FROM FCCELL ");//EMPLOYEE is the table name in the toptais database.

      /* This enumerates all of the rows in the ResultSet and 
         prints out the values at the columns named ID, NAME, 
         LOCATION. 
      */
      while (rs.next()) {
        out.println(rs.getString("FSTRTABLENAME") + "- " + 
          rs.getString("FSTRCLASS") + "- " + "<p>");
      }

      /* Release the ResultSet and Statement.
       */
      rs.close();
      stmt.close();

    } catch (Exception E) {
      /* 
         Handle exception here. 
      */
      System.out.println("Service Error: " + E);
    } finally {
      if (rs != null) {
        try { rs.close(); } catch (Exception ignore) {};
      }
      if (stmt != null) {
        try { stmt.close(); } catch (Exception ignore) {};
      }
      if (myConn != null) {
        try { myConn.close(); } catch (Exception ignore) {};
      }
    }
  }

  /* 
   * Local Variables 
   */
  Context ctx;  
  DataSource ds;
  Statement stmt;
  ResultSet rs;

}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -