使用 Java 准备语句的 Sybase 查询不起作用
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
字符串比较失败可能是因为数据库列中填充了空格。
另一项建议。如果您确信它总是返回一行(或者零 )。
String compare is failing possibly because of spaces being padded to the database column.
One other suggestion. Instead of doing a
while(results.next())
, do anif(results.next())
if you are confident it will always return exactly one row ( or zero ).