JSP調用SQL server 2000存儲過程_JSP教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:使用FileWriter可以寫UTF-8的解決方法
      FileWriter不能寫utf-8,相信好呢多新手都遇到過吧,今天我們就來解決這個問題,看下面的例子。 package cn.yethyeth.sample.io; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.Outp

      1.新建表:

      CREATE TABLE [mytest] (
      [id] [int] NOT NULL ,
      [name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
      [phone] [varchar] (13) COLLATE Chinese_PRC_CI_AS NULL ,
      [addr] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
      ) ON [PRIMARY]
      GO

      2.新建存儲過程:

      create procedure get_info @name varchar(50),@phone varchar(13),@addr varchar(50)
      as
      select *
      from mytest
      go

      3.JSP代碼:

      <%@ page language="java" pageEncoding="UTF-8" import="java.sql.*,tools.Database"%>
      <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" divfix="bean" %>
      <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" divfix="html" %>
      <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" divfix="logic" %>
      <%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" divfix="tiles" %>
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html:html locale="true">
      <head>
      <html:base />
      <title>proc_test.jsp</title>
      <meta http-equiv="pragma" content="no-cache">
      <meta http-equiv="cache-control" content="no-cache">
      <meta http-equiv="expires" content="0">
      <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
      <meta http-equiv="description" content="This is my page">
      </head>
      <body>
      This is a test for procedure. <br>
      <%
      String username="sa";
      String password = "123456";
      String url = "jdbc:microsoft:sqlserver://192.168.1.112:1433;DatabaseName=test";
      Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
      Connection conn = DriverManager.getConnection(url, username, password);
      String sql = "execute get_info";
      //創建一個CallableStatement 對象來調用數據庫存儲過程
      //CallableStatement comm = conn.divpareCall(sql);
      Statement stmt = conn.createStatement();

      ResultSet res = stmt.executeQuery(sql);
      while(res.next())...{
      %>
      <table>
      <tr>
      <td><%=res.getString("id")%></td>
      <td><%=res.getString("name")%></td>
      <td><%=res.getString("phone")%></td>
      <td><%=res.getString("addr")%></td>
      </tr>
      </table>
      <%}
      res.close();
      stmt.close();
      conn.close();
      %>
      </body>
      </html:html>

      4.執行結果:

      This is a test for procedure.
      1 cc 1234 cs
      2 dd 4567 bj
      3 ee 1245 ca

      分享:關于Jsp頁面的幾種傳參方式
      1.JavaScript傳參:這種傳參方式用opener關鍵字, 可以實現跨頁傳參.其用法就是用opener關鍵字調用父窗體的一個組件. 舉例: opener.myform.txt.value = document.myform.txt.value; 優點: 簡單,對網絡傳輸限制比較底. 缺點: 不靈活,對與大量的傳參不現實,也不

      來源:模板無憂//所屬分類:JSP教程/更新時間:2012-06-08
      相關JSP教程