使用在存储过程中存储视图

时间:2012-02-02 11:17:40

标签: java sql-server-2008 stored-procedures

我有一个使用存储过程的java代码:

ALTER PROCEDURE [dbo].[Search_Rent]
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    select * from SearchRent    
END

SearchRentsql server

中的视图
SELECT     dbo.Tbl_Software.ID AS SotwareID, dbo.Tbl_Software.UserID_Rent, dbo.Tbl_Software.Name AS SoftwareName, dbo.Tbl_User.Family, 
                      dbo.Tbl_User.Name AS UserName, dbo.Tbl_User.Tel, dbo.Tbl_Software.DataStart, dbo.Tbl_Software.DataFinish, dbo.Tbl_Software.ID_Category
FROM         dbo.Tbl_Software INNER JOIN
                      dbo.Tbl_User ON dbo.Tbl_Software.UserID_Rent = dbo.Tbl_User.ID

我的java代码是:

public class frmRented extends javax.swing.JFrame {

    /** Creates new form frmRented */
    public frmRented() {
        initComponents();
        MyTools.SetCenterLocation(this);
        DataAccess da = new DataAccess();
        try {
            da.Connect();

            //da.rs = da.st.executeQuery("select * from SearchRent");
            //da.rs = da.st.executeQuery("{call Search_Rent()}");
            CallableStatement cal = da.con.prepareCall("{call Search_Rent()}",ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            da.rs = cal.executeQuery();

            da.rs.last();
            int rc = da.rs.getRow();
            da.rs.beforeFirst();
            int cc = 9;

            String[] header = {"id", "idc", "name", "preamble", "k ", "s", "d", "a","e"};
            String[][] data = new String[rc][cc];
            int i = 0;

            while (da.rs.next()) {
                data[i][0] = da.rs.getString("Tbl_Software.ID");
                data[i][1] = da.rs.getString("Tbl_Software.ID_Category");
                data[i][2] = da.rs.getString("Tbl_Software.Name");
                data[i][3] = da.rs.getString("Tbl_Software.UserID_Rent");
                data[i][4] = da.rs.getString("Tbl_User.Name");
                data[i][5] = da.rs.getString("Tbl_User.Family");
                data[i][6] = da.rs.getString("Tbl_User.Tel");
                data[i][7] = da.rs.getString("Tbl_Software.DataStart");
                data[i][8] = da.rs.getString("Tbl_Software.DataFinish");
                i++;

            }
            jTable1 = new JTable(data, header);
            jScrollPane1.setViewportView(jTable1);
            cal.close();
            da.Disconnect();
        } catch (Exception e) {
            System.out.println(e);
        }

    }

但是当我执行此代码时出现此错误:

  

com.microsoft.sqlserver.jdbc.SQLServerException:列名Tbl_Sofware.ID无效。

如何解决此错误?

2 个答案:

答案 0 :(得分:0)

替换

CallableStatement cal = da.con.prepareCall("{call Search_Rent()}",);

使用

CallableStatement cal = da.con.prepareCall("{call Search_Rent()}",ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

我认为你需要这个,因为这行

da.st = da.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

没有做任何事情,您创建此Statement,并将其分配给da.st,但之后不要使用它。您实际上是使用CallableStatement

执行查询

答案 1 :(得分:0)

您的观点未正确宣布。尝试以唯一的方式从视图中运行SELECT语句,并在成功时,使用视图包装结果语句。

在通过阅读器读取值时无需指定表别名,以适当的方式替换代码 AND 您必须从视图中引用实际列名称:

data[i][0] = da.rs.getString("SotwareID");
data[i][1] = da.rs.getString("ID_Category");
data[i][2] = da.rs.getString("SoftwareName");
data[i][3] = da.rs.getString("UserID_Rent");
data[i][4] = da.rs.getString("UserName");
data[i][5] = da.rs.getString("Family");
data[i][6] = da.rs.getString("Tel");
data[i][7] = da.rs.getString("DataStart");
data[i][8] = da.rs.getString("DataFinish");