使用 Java 准备语句的 Sybase 查询不起作用

发布于 2024-11-26 18:09:14 字数 1860 浏览 2 评论 0原文

我有一个使用 Java/JSP 的 Web 应用程序,该应用程序在 MySQL 数据库上运行,现在我已将其移至 Sybase。

我已经更改了我所认为的所有相关连接部分(Sybase 连接器和相关代码),

我一直在使用PreparedStatements 进行查询。 MySQL 中运行的内容现在不再返回任何内容(错误或结果)。

我测试的第一个简单查询是一个登录表单,它应该与表“appuser”中的用户名和密码值匹配。

提供的代码中的准备语句曾经可以工作,但不再适用于 Sybase。

相关代码如下。如果还需要什么,请告诉我。

public void connectDB() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException
{
    /* String userName="****";
    String password="******";
    String url="jdbc:sybase:sybdev1dw/ETL_MON";
    Class.forName("com.sybase.jdbc4.jdbc.SybDriver").newInstance();
    conn=DriverManager.getConnection(url, userName, password); */

    String driver = "com.sybase.jdbc2.jdbc.SybDriver";
    String host = "********";
    String dbName = "********";
    String url = "jdbc:sybase:Tds:" + host  + ":4110" +"?SERVICENAME=" + dbName;
    String username = "*******";
    String password = "*********";

    Class.forName(driver);
    conn = DriverManager.getConnection(url, username, password);
    System.out.println("Connection successful"+conn);

}

public Boolean loginAction(String name, String pass, String role,HttpServletRequest request) throws SQLException {
    Boolean valid=false;
    session=request.getSession();
    PreparedStatement stmt=conn.prepareStatement("SELECT pass FROM appuser WHERE name=?");
    stmt.setString(1, name);
    results=stmt.executeQuery();

    System.out.println("loginAction:"+results.getFetchSize());

    while(results.next())
    {
        if(results.getString("pass").equals(pass)) 
        {
            valid=true;
            session.setAttribute("name",name);
            session.setAttribute("role",role);
        }
        else
        {
            valid=false;
        }
    }
    System.out.println("loginAction:"+valid);

    return valid;
}

I have a web application using Java/JSP that was running off a MySQL database which I've now moved to Sybase.

I've changed what I believe are all the relevant connection parts (Sybase Connector and relevant code)

I've been using PreparedStatements for my queries. What was working in MySQL is now no longer returning anything (errors or results).

The first simple query I've been testing is a login form which should be matching the user values for name and password in the table "appuser".

The Prepared Statement in the code provided used to work but no longer does with Sybase.

Relevant code is below. Let me know if anything else is needed.

public void connectDB() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException
{
    /* String userName="****";
    String password="******";
    String url="jdbc:sybase:sybdev1dw/ETL_MON";
    Class.forName("com.sybase.jdbc4.jdbc.SybDriver").newInstance();
    conn=DriverManager.getConnection(url, userName, password); */

    String driver = "com.sybase.jdbc2.jdbc.SybDriver";
    String host = "********";
    String dbName = "********";
    String url = "jdbc:sybase:Tds:" + host  + ":4110" +"?SERVICENAME=" + dbName;
    String username = "*******";
    String password = "*********";

    Class.forName(driver);
    conn = DriverManager.getConnection(url, username, password);
    System.out.println("Connection successful"+conn);

}

public Boolean loginAction(String name, String pass, String role,HttpServletRequest request) throws SQLException {
    Boolean valid=false;
    session=request.getSession();
    PreparedStatement stmt=conn.prepareStatement("SELECT pass FROM appuser WHERE name=?");
    stmt.setString(1, name);
    results=stmt.executeQuery();

    System.out.println("loginAction:"+results.getFetchSize());

    while(results.next())
    {
        if(results.getString("pass").equals(pass)) 
        {
            valid=true;
            session.setAttribute("name",name);
            session.setAttribute("role",role);
        }
        else
        {
            valid=false;
        }
    }
    System.out.println("loginAction:"+valid);

    return valid;
}

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

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

发布评论

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

评论(1

万劫不复 2024-12-03 18:09:14

字符串比较失败可能是因为数据库列中填充了空格。

result.getString("pass").trim().equals(pass.trim()) 

另一项建议。如果您确信它总是返回一行(或者零 )。

String compare is failing possibly because of spaces being padded to the database column.

result.getString("pass").trim().equals(pass.trim()) 

One other suggestion. Instead of doing a while(results.next()) , do an if(results.next()) if you are confident it will always return exactly one row ( or zero ).

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