Usar almacenar una vista en un procedimiento almacenado

I have a java code that use a stored procedure for that it is:

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

y la SearchRent is a view in sql 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

y mi código java es:

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);
        }

    }

but when I execute this code I have this error:

com.microsoft.sqlserver.jdbc.SQLServerException: The column name Tbl_Sofware.ID is not valid.

¿Cómo puedo solucionar este error?

preguntado el 02 de febrero de 12 a las 11:02

Can you give us the stack trace, or tell us what line is throwing the error? I'd assume it is the call to beforeFirst, but more information would be good. -

Cambiar ResultSet.CONCUR_READ_ONLY a ResultSet.CONCUR_UPDATABLE -

por favor, aprenda las convenciones de nomenclatura de Java y cúmplalas -

2 Respuestas

Your view is not properly declared. Try to run SELECT statement from view in sole manner, and when you succeed, wrap the resulting statement with view.

OR

you need not specify the table alias while reading values thru reader, replace your code with this in appropriate manner y you have to refer real column names from your view:

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");

Respondido 02 Feb 12, 15:02

this code too have same exception: com.microsoft.sqlserver.jdbc.SQLServerException: The column name ID is not valid. - Hambriento

Sorry, not mentioned the real names of columns in view. Updated answer. - Oleg Dok

ooohhhh,com.microsoft.sqlserver.jdbc.SQLServerException: The column name SoftwareID is not valid. - Hambriento

Be careful - your column name if SotwareID - without a f letter! - Oleg Dok

Replace

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

Con

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

I think you need this because this line

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

Isn't doing anything, you create this Statementy asígnelo a da.st but then don't use it. You actually execute your query using the CallableStatement

Respondido 02 Feb 12, 14:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.