简体   繁体   English

Servlet中的SQL语句无效

[英]invalid sql statement in servlet

In login page i make validation that user is allowed to enter system , i make methode which validate user: 在登录页面中,我确认允许用户进入system,我确定要验证用户的方法:

 boolean isValidUser(Connection con,String userName,String pass ){}

it works correctly in desktop Application, but when i tried it in servlet it makes exception that table or view doesn't exist ??? 它可以在桌面应用程序中正常工作,但是当我在servlet中尝试它时,它会导致表或视图不存在的异常? but the table is aleady exist in my db ?? 但是表在我的数据库中存在铅? Can somebody tell me where is the problem? 有人可以告诉我问题出在哪里吗?

this is the method 这是方法

public boolean isValidUser(Connection con,String userName,String pass )throws SQLException
{
    String selSQL="SELECT USER_NAME, USER_PASS FROM OSQS_USERS where USER_NAME =? and USER_PASS =?";
    ResultSet rs =null;
    boolean exist =false;
    PreparedStatement pstmt = null;
    try {

        pstmt = con.prepareStatement(selSQL);
        pstmt.setString(1,userName);
        pstmt.setString(2, pass);

        rs=pstmt.executeQuery();
        if(rs.next())
            exist= true;

    }
//close statment and result sest 
return exist;
}

Given that the same code executes correctly in the context of a desktop application, you might want to check the JDBC connectivity details that are being utilized to retrieve the connection in the servlet. 假定在桌面应用程序的上下文中可以正确执行相同的代码,则可能要检查用于检索servlet中的连接的JDBC连接详细信息。

This is important in the case of databases like Oracle, since multiple database user accounts can exist in a single database installation. 这对于像Oracle这样的数据库而言非常重要,因为在一个数据库安装中可以存在多个数据库用户帐户。 It is quite possible that a separate Oracle user account was utilized in the context of the database application and another in the context of the servlet. 很可能在数据库应用程序的上下文中使用了一个单独的Oracle用户帐户,而在servlet的上下文中则使用了另一个。 In Oracle, database objects are owned by a particular user, and if another account wishes to access the same, then privileges must be granted to the second account. 在Oracle中,数据库对象由特定用户拥有,并且如果另一个帐户希望访问该对象,则必须将特权授予第二个帐户。

If you cannot change the credentials used to access the database from the servlet, consider granting the necessary privileges (SELECT in this case) to the second user, although I must admit that it would not solve your problem completely. 如果您不能更改用于从Servlet访问数据库的凭据,请考虑向第二个用户授予必要的特权(在这种情况下为SELECT),尽管我必须承认这样做不能完全解决您的问题。 The second database user account, would require rights on more or less all the objects owned by the first, for it is unlikely that your application was designed to use multiple database accounts. 第二个数据库用户帐户或多或少要求第一个数据库用户帐户拥有权限,因为您的应用程序不太可能被设计为使用多个数据库帐户。

If Oracle is telling you that a table or view "does not exist", then it is telling you that it cannot find them ... even if you think that it should find them. 如果Oracle告诉你,一个表或视图“不存在”,则是告诉你, 不能找到他们......即使认为它应该找他们。

I would hazard a guess that you are connecting to a different Oracle database when running in your desktop application and in a servlet. 我可能会猜测您在桌面应用程序和servlet中运行时将连接到另一个Oracle数据库。 This may be by design or by accident. 这可能是设计使然,也可能是偶然的。

Take a look at your respective JDBC configurations; 看一下您各自的JDBC配置; specifically the connection URLs. 特别是连接网址。

(It is also possible that this is a permissions problem. But I'd have thought that Oracle would tell you that you don't have permission to use the table or view ... rather than telling you that it doesn't exist.) (这也可能是权限问题。但是我以为Oracle会告诉您您没有使用表或视图的权限...而不是告诉您它不存在。 )

EDIT 编辑

I suspect that the root problem is that the 'localhost' in the JDBC URL is sending you to different hosts (and hence Oracle database servers) in the desktop and servlet cases. 我怀疑根本问题在于,在桌面和servlet情况下,JDBC URL中的“ localhost”会将您发送到其他主机(因此也将您发送到其他主机)。 Try using the actual DNS name of the server that contains the database you are trying to use. 尝试使用包含您要使用的数据库的服务器的实际DNS名称。

if yes,where should i add data to orcle db to make testing for my application 如果是,我应该在哪里向Orcle db添加数据以对我的应用程序进行测试

You should be asking your local database administrator that question! 您应该问本地数据库管理员这个问题!

数据库用户可能是与表所有者不同的用户,因此,除非您授予对该表的选择权限(并为所有引用加上前缀:tablename-> owner.tablename),否则Oracle不会允许您访问该表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM