db2如何创建简单更新并选择存储过程?

时间:2016-02-03 08:28:40

标签: stored-procedures db2

DB2存储过程更新记录并选择记录

CREATE PROCEDURE DB2INST1.GETPEOPLE2(IN ids bigint )

    SPECIFIC DB2INST1.GETPEOPLE2

    DYNAMIC RESULT SETS 1

    MODIFIES SQL DATA

    LANGUAGE SQL 



    BEGIN 

        update test2 set a=a+1 where a>ids;

         DECLARE rs1 CURSOR WITH RETURN TO CLIENT FOR  

        select * from db2inst1.test2;

        OPEN rs1;
END  

但它没有用。 错误:DB21034E该命令作为SQL语句处理,因为它不是有效的命令行处理器命令。在SQL处理期间,它返回:SQL0104N一个意外的令牌" rs1 CURSOR sele"在" ID之后被发现; DECLARE&#34 ;.预期的代币可能包括:""。 LINE NUMBER = 10。 SQLSTATE = 42601

1 个答案:

答案 0 :(得分:1)

好的,它有效:

The class 

import java.sql.ResultSet;
import java.sql.SQLException;

public class Atleta {
    private int socio;
    private String nome;
    private String sexo;



    public void setSocio(int soc){
        this.socio=soc;
    }
    public long getSocio(){
        return(this.socio);
    }

    public void setNome(String nom){
        this.nome=nom;
    }
    public String getNome(){
        return (this.nome);
    }

    public void setSexo(String sex){
        this.sexo=sex;
    }
    public String getSexo(){
        return (this.sexo);
    }

    public Atleta (ResultSet resultSet) throws SQLException
    {
        this.socio = resultSet.getInt("socio");
        this.nome = resultSet.getString("nome");
        this.sexo = resultSet.getString("sexo");

    }
}



The servlet

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import appClass.Atleta;
import dbconn.dbconn;
import javax.servlet.RequestDispatcher;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String qryStr = "";
            Statement stmt = null;
            ResultSet rs = null;
            //res.setContentType("text/html");
            //PrintWriter out = res.getWriter();
            PrintWriter out = response.getWriter();
            Connection conn = null;
               //using movie_resolved view
            try{
              // Get a Connection to the database
             conn = dbconn.getConnection();
             // Create a Statement object
              stmt = conn.createStatement();
              // Execute an SQL query, get a ResultSet
              qryStr = "SELECT socio, nome, sexo from atletas";

              rs = stmt.executeQuery(qryStr); 

             if (rs.next())
              {

                Atleta atleta = new Atleta (rs);
                atleta.setSocio(rs.getInt("socio"));
                atleta.setNome(rs.getString("nome"));
                atleta.setSexo(rs.getString("sexo"));
                request.setAttribute("atleta", atleta);
                RequestDispatcher view = request.getRequestDispatcher("atleta.jsp");
                view.forward(request, response);
              }

            }
            catch(Exception e) {
             out.println("Couldn't load database driver: " + e.getMessage());
            }

              try {
                if (conn != null) conn.close();
              }
              catch (SQLException ignored) { }
           }
          }

The JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page language="java" import="java.sql.*" import="java.util.*" %>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">


        <title>JSP Page</title>
    </head>
    <body>
        <h2>Lista de Atletas!</h2>

        <table border="1">


        <TR>
            <TD>Socio: </TD>
            <TD>${atleta.socio}</TD>
        </TR>

        <TR>
            <TD>Lastname: </TD>
            <TD>${atleta.nome}</TD>
        </TR>
         <TR>
            <TD>Firstname: </TD>
            <TD>${atleta.sexo}</TD>
        </TR>
        </table>

    </body>
</html>