[jsp] JSP 連接oracle DB

最近碰到一個新需求,是要使用JSP來連接oracle DB,取得資料。
以下是連接oracle DB某資料表,並逐列把第一欄資料print出的原始碼:

<%@ page contentType="text/html;charset=big5"%> 
<%@ page import="java.sql.*"%> 

<%
    try 
    {  
        //取得參數
        String myPage = request.getParameter("Page");
        out.println(myPage + "<br>");
        
        String[] pagePara = myPage.split(",");
        out.println(pagePara[0] + "<br>");
        out.println(pagePara[1] + "<br>");
        out.println(pagePara[2] + "<br>");
    
        //連接資料庫(需要IP、PORT、DB_SID)
        Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); 
        String url="jdbc:oracle:thin:@IP:PORT:DB_SID"; 
        
        String user=""; 
        String password=""; 
        Connection conn= DriverManager.getConnection(url,user,password); 
        Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
        
        String sql="SELECT * FROM XXX"; 
        ResultSet rs=stmt.executeQuery(sql); 
        
        String tempStr = "";
        while(rs.next()) 
        {
%> 
            col(1)=:<%=rs.getString(1)%><p>
        ---------------------------------------<p>
<%
        }
        out.print("數據庫操作成功!");
        rs.close(); 
        stmt.close(); 
        conn.close();
            
    }

    catch(Exception e)
    {
        //out.println("An exception occurred: " + e.getMessage());
        out.println(e);
    }

%> 


較新的 較舊