SQL查询出问题啦 求救啊

来源:百度知道 编辑:UC知道 时间:2024/05/10 09:30:14
条件:ad表内有id(int),name(char),password(char);执行查询判断。已经有基本语句。
String result = null;String result1 = null;String password = "admin";
String command = null;String command1 = null;String name = "admin";
command = "select name from ad ";
rs = sm.executeQuery(command); // 执行查询
do {
result = rs.getString(name);
if (result.equals(name)) {
command1 = "select "+password+" from ad where name=" + "'"+name+"'";
rs1 = sm.executeQuery(command);
result1 = rs.getString(password);
if (result1.equals(password)) {
a = 0;
break;
}
}
} while (rs.next());
} catch (SQLException ex) {
System.out.println("SQLException:");
while (ex != null) {
System.out.println("Message:" + ex.getMessage());
ex = ex.getNextException();
}
} catch (Exception e) {
System.out.println("IOExcep

建议这样写:
Class.forName(...);
Connection conn = DriverManager.getConnection();
String sql = "select count(*) from users where uname=? and password=?";
PreparedStatement ps = conn.preparedStatement(sql);
ps.setString(1, uname);
ps.setString(2, password);
int n = ps.executeUpdate();
return n>0 ? n : -1;
记得要捕获异常啊

"select "+password+" from ad where name=" + "'"+name+"'";

改成:"select password from ad where name=" + "'"+name+"'";

String password = "admin";
你的表没有admin这个字段

name=" + "'"+name+"'; 改成 name='"+name+"';

command1 = "select "+password+" from ad where name=" + "'"+name+"'";
换成
command1 = "select password from ad where name=" + "'"+name+"'";