jsp和javabean的交叉使用,jspjavabean使用

來源:互聯網
上載者:User

jsp和javabean的交叉使用,jspjavabean使用

很多時候我們在jsp編程時,會出現大量重複的代碼,這時候就需要使用javabean。下面我給大家介紹jsp檔案使用javabean

目錄結構

 




只需要在jsp檔案的開始加上紅色的一段代碼即可

使用時直接使用

<%@ page language="java"import="java.util.*,java.sql.*"pageEncoding="UTF-8"%>

<%

String path =request.getContextPath();

String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<jsp:useBean id="DBJB" scope="page"class="lytjb.DB"/>

<!DOCTYPEHTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">

<html>

  <head>

    <title>My JSP 'index.jsp'starting page</title>

  </head>

………

………..

……….

String sql="selectbook_picture,book_name,book_out_price,book_stock from college_info wherecollege='政法 '";

       ResultSet rs =DBJB.executeQuery(sql);//返回SQL語句查詢結果集(集合)

 

…………………….

 

 

附有DB.Java代碼

package lytjb;

import java.sql.*;

//一個用於尋找資料來源的工具類。

public class DB {

    private Connectioncon = null;

    private Statementstmt = null;

    ResultSet rs = null;

 

    public ResultSet executeQuery(String sql)throws Exception {

          

           try{ 

           Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 

       con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=SHB","sa","1234567"); 

           //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

           //con=DriverManager.getConnection("jdbc:odbc:JDBCSQLDemo_JSPTest"); 

           stmt=con.createStatement(); 

           rs=stmt.executeQuery(sql);

           }catch(Exception e){}

       returnrs;

    }

 

    // 執行Insert,Update語句

    public void executeUpdate(String sql) throws Exception {

       try{ 

           Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 

       con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=SHB","sa","1234567"); 

           //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

           //con=DriverManager.getConnection("jdbc:odbc:JDBCSQLDemo_JSPTest"); 

           stmt=con.createStatement(); 

           intrs=stmt.executeUpdate(sql);

          

           }catch(Exception e){}

    }

 

    // 關閉stmt和關閉串連

    public void close_all() {

       try {

           stmt.close();

           con.close();

       } catch (SQLException e) {

           e.printStackTrace();

       }

    }

 

}

 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.