JBuilder9+Weblogic7ʵսƪ֮JSPʹÓÃJDBCƪ

Àà±ð£ºJava µã»÷£º0 ÆÀÂÛ£º0 ÍƼö£º

JBuilder9+Weblogic7ʵսƪ

JSPͨ¹ýjavaBeanµ÷ÓÃJDBCƪ

                                                                             ×÷Õߣº»Æ ¿­        

E_mail£º[email protected]

   

ÔËÓÃƪ JSP+javaBean+JDBC+Oracle

Ò»¡¢Æô¶¯JBuilder

Æô¶¯JBuilder£º ¿ªÊ¼/³ÌÐò/Borland JBuilder 9 Enterprise Trial/Borland JBuilder 9 Enterprise Trial£»

 

ÔÚ²Ù×÷ÏÂÃæµÄ²½ÖèÇ°ÇëÏȼì²éÒ»ÏÂÄãµÄÅäÖúͲÎÊýÊÇ·ñÓ롶JBuilder9+Weblogic7ʵսƪ֮¹¤¾ßƪ£¨ORACLE9i£©¡·¡¢¡¶JBuilder9+Weblogic7ʵսƪ֮¹¤¾ßƪ£¨JDBC 1£©¡·Ò»Ö¡£

ÒÔÏÂûÓÐÏêϸ˵Ã÷²¿·ÖÈçÓв»Ã÷°×µÄÇë²Î¿´¡¶JBuilder9+Weblogic7ʵսƪ֮JSP+SERVLETƪ¡·¡£

¶þ¡¢´´½¨Ò»¸ö¹¤³Ì

    ´´½¨Ò»¸öÃûΪsearchµÄ¹¤³Ì¡£

 

Èý¡¢ÉèÖà Web Application

    ´´½¨Ò»¸öÃûΪsearchµÄWeb Application¡£

 

ËÄ¡¢´´½¨ JSPs

4.1´´½¨search.jspÎļþ£»

4.2ÔÚ´úÂëÇøËùÓдúÂëÓÃÏÂÃæ´úÂëÌæ»»£º

<%@ page contentType="text/html; charset=GBK"

     import="java.util.*" import="java.sql.ResultSet" import="search.dbBean"%>

<html>

<head>

<title>

search

</title>

</head>

<body bgcolor="#ffffff">

<center>

<h1>

JSP-->javaBean-->JDBC-->Oracle

</h1>

</center>

<%

dbBean conn = new dbBean();

ResultSet rs = null;

rs = conn.executeQuery("SELECT * FROM count");

ArrayList validates = new ArrayList();

while (rs.next())

{

    String num1 = rs.getString("num1").trim();

        String num2 = rs.getString("num2").trim();

    String result = rs.getString("result").trim();

%>

<table width="100%" cellspacing="0" cellpadding="5" align="center">

<tr>

<td><B>num1</b></td>

<td><B>num2</b></td>

<td><B>result</b></td>

</tr>

<tr>

<td><%=num1%></td>

<td><%=num2%></td>

<td><%=result%></td>

</tr>

</table>

<%

}

conn.closeConnection();

%>

</body>

</html>

 

Îå¡¢´´½¨ javaBean

5.1Ôڲ˵¥À¸Ñ¡ÔñFile/New...£¬µ¯³öObject Gallery´°¿Ú£»

5.2µ¥»÷GeneralÑ¡Ï£»

5.3Ñ¡ÔñJavaBeanÈ»ºóµ¥»÷OK£¬³öÏÖJavaBeanµÄÏòµ¼Ò³Ã棻

5.4ÔÚClass nameºóÊäÈëdbBean£»µ¥»÷OKÍê³É¡£

5.5ÔÚ´úÂëÇø½«ËùÓдúÂëÓÃÒÔÏ´úÂëÌæ»»£º

package search;

 

import java.io.*;

import java.sql.*;

import java.util.*;

import javax.naming.*;

import javax.sql.*;

 

public class dbBean

    implements Serializable

{

    DataSource ds = null;

    Context ctx = null;

    Connection conn = null;

    Statement stmt = null;

    ResultSet rs = null;

 

    public void getConnection()

    {

        try

        {

            //»ñµÃWeblogic Server JBDI³õʼÉÏÏÂÎÄÐÅÏ¢

            ctx = getInitialContext();

            System.out.println(ctx.toString());

            //²éÕÒJNDIÃû×ÖΪjdbc/testTxDataSourceµÄÊý¾ÝÔ´£¬½¨Á¢Êý¾ÝÔ´¶ÔÏó

            ds = (javax.sql.DataSource) ctx.lookup("jdbc/testTxDataSource");

            System.out.println(ds.toString());

            //ͨ¹ýÊý¾ÝÔ´¶ÔÏó½¨Á¢Êý¾ÝÁ¬½Ó

            conn = ds.getConnection();

            System.out.println(conn.toString());

            //½¨Á¢Óï¾ä¶ÔÏó

            stmt = conn.createStatement();

        }

        catch (java.sql.SQLException es)

        {

            System.out.println("mySQLException" + es.getMessage());

        }

        catch (Exception ex)

        {

            System.out.println("conn is error" + ex.getMessage());

        }

    }

 

    public void closeConnection()

    {

        try

        {

            if (rs != null)

                //¹Ø±Õ½á¹û¼¯

                rs.close();

            if (stmt != null)

                //¹Ø±ÕÓï¾ä¶ÔÏó

                stmt.close();

            if (conn != null)

                //¹Ø±ÕÁ¬½Ó

                conn.close();

        }

        catch (java.sql.SQLException e)

        {

            System.out.println("mycloseConnectioinSQL" + e.getMessage());

            rs = null;

            stmt = null;

            conn = null;

        }

    }

 

    public ResultSet executeQuery(String sql)

    {

        rs = null;

        try

        {

            if (conn == null)

                getConnection();

            if (conn != null)

                stmt = conn.createStatement();

            rs = stmt.executeQuery(sql);

        }

        catch (Exception e)

        {

            System.out.println("executeQuery error here: " + e.getMessage());

        }

        return rs;

    }

 

    public boolean executeUpdate(String sql)

    {

        boolean bupdate = false;

        try

        {

            if (conn == null)

                getConnection();

            if (conn != null)

            {

                stmt = conn.createStatement();

                int rowCount = stmt.executeUpdate(sql);

                if (rowCount != 0)

                    bupdate = true;

            }

        }

        catch (SQLException ex)

        {

            System.err.println("Conn.executeUpdate" + ex.getMessage());

        }

        return bupdate;

    }

 

    //getInitialContext()·½·¨ÓÃÀ´»ñµÃWeblogic ServerµÄ³õʼÉÏÏÂÎÄ»·¾³ÒÔ½øÐÐJNDI²éѯ¡£

    private static Context getInitialContext() throws Exception

    {

        //url¡¢user¡¢password·Ö±ð¶ÔÓ¦Weblogic ServerµÄµØÖ·¡¢¶Ë¿ÚºÅ¡¢ÏµÍ³Óû§¼°¿ÚÁî

        String url = "t3://localhost:7001";

        String user = "training";

        String password = "training";

        Properties properties = null;

        try

        {

            properties = new Properties();

            properties.put(Context.INITIAL_CONTEXT_FACTORY,

                           "weblogic.jndi.WLInitialContextFactory");

            properties.put(Context.PROVIDER_URL, url);

            properties.put(Context.SECURITY_PRINCIPAL, user);

            properties.put(Context.SECURITY_CREDENTIALS, password);

            return new InitialContext(properties);

        }

        catch (Exception e)

        {

            throw e;

        }

    }

 

    /*Bean²âÊÔÓÃ

    public static void main(String args[])

    {

        ResultSet rs = null;

        try

        {

            dbBean connData = new dbBean();

            //½¨Á¢½á¹û¼¯¶ÔÏó

            rs = connData.executeQuery("SELECT * FROM count");

            //±éÀú½á¹û¼¯¶ÔÏ󣬷ÃÎÊÿһÌõ¼Ç¼£¬Êä³öÊý¾Ý

            while (rs.next())

            {

                String num1 = rs.getString("num1").trim();

                String num2 = rs.getString("num2").trim();

                String result = rs.getString("result").trim();

                System.out.println("validate: \'" + num1 + "+" + num2 + "=" +

                                   result + "\" is ");

            }

        }

        catch (Exception e)

        {}

    }*/

}

    Íê³ÉºóÔËÐв鿴½á¹û¡£

 

ÎÒµÄÎÄÕÂÊ×·¢Å£¶úÂÛ̳£¨www.newer.com.cn/bbs£©ºÍ³ÌÐòÔ±ÂÛ̳£¨www.csdn.net£©£¬»¶Ó­×ªÔØ£¬²»¹ý£¬Çë±£Áô×÷ÕßÒÔ¼°ÐÞ¶©ÕßµÄÃû×Ö£¬Ð»Ð»¡£

 

±¾ÎĵØÖ·£ºhttp://com.8s8s.com/it/it17224.htm