servlet 中的 sql 语句无效

发布于 2024-09-18 11:40:47 字数 812 浏览 7 评论 0原文

在登录页面中,我验证用户是否可以进入系统, 我制作了验证用户的方法:

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

它在桌面应用程序中正常工作,但是当我在 servlet 中尝试它时,它会出现异常,表或视图不存在???但该表已经存在于我的数据库中? 有人能告诉我问题出在哪里吗?

这就是方法

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

In login page i make validation that user is allowed to enter system ,
i make methode which validate user:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还如梦归 2024-09-25 11:40:48

鉴于相同的代码在桌面应用程序的上下文中正确执行,您可能需要检查用于检索 servlet 中的连接的 JDBC 连接详细信息。

这对于 Oracle 等数据库非常重要,因为单个数据库安装中可以存在多个数据库用户帐户。很有可能在数据库应用程序上下文中使用单独的 Oracle 用户帐户,而在 servlet 上下文中使用另一个帐户。在 Oracle 中,数据库对象由特定用户拥有,如果另一个帐户希望访问该对象,则必须向第二个帐户授予权限。

如果您无法更改用于从 servlet 访问数据库的凭据,请考虑向第二个用户授予必要的权限(在本例中为 SELECT),尽管我必须承认这不能完全解决您的问题。第二个数据库用户帐户将需要对第一个数据库用户帐户拥有的或多或少的所有对象的权限,因为您的应用程序不太可能设计为使用多个数据库帐户。

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.

This is important in the case of databases like Oracle, since multiple database user accounts can exist in a single database installation. 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. 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.

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. 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.

我纯我任性 2024-09-25 11:40:48

如果 Oracle 告诉您某个表或视图“不存在”,那么它就是在告诉您无法找到它们...即使认为它应该找到它们找到他们。

我大胆猜测您在桌面应用程序和 servlet 中运行时正在连接到不同的 Oracle 数据库。这可能是有意为之,也可能是偶然。

查看一下各自的 JDBC 配置;特别是连接 URL。

(也有可能这是一个权限问题。但我以为Oracle会告诉你你没有权限使用该表或视图......而不是告诉你它不存在。 )

编辑

我怀疑根本问题是 JDBC URL 中的“localhost”将您发送到桌面和 servlet 情况下的不同主机(以及 Oracle 数据库服务器)。尝试使用包含您尝试使用的数据库的服务器的实际 DNS 名称。

如果是,我应该在哪里将数据添加到 orcle db 来测试我的应用程序

您应该问您本地的数据库管理员这个问题!

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.

I would hazard a guess that you are connecting to a different Oracle database when running in your desktop application and in a servlet. This may be by design or by accident.

Take a look at your respective JDBC configurations; 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.)

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. Try using the actual DNS name of the server that contains the database you are trying to use.

if yes,where should i add data to orcle db to make testing for my application

You should be asking your local database administrator that question!

回心转意 2024-09-25 11:40:48

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

Your database user is probably a different user from the table's owner, so unless you grant select permissions to that table (and prefix all references: tablename -> owner.tablename) Oracle is not going to let you access that table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文