看看下面的代码哪里错了,为什么得到的结果不一样?//使用Statement测试
public class DBUtil1 {
// 构造函数
public DBUtil1(){ } // 登录验证
public String getUser(String userID,String userPass){
// 声明一字符,用来返回用户类型
String userType = null;
// 声明一查询语句
String sql = "select USER_ROLE from user_info where USER_ID = '" + userID + "' and USER_PASS = '" + userPass + "'";
// 声明一Connection
Connection conn = null;
// 声明一statement
Statement st = null;
// 声明一resultSet
ResultSet rs = null;
try{
// 建立连接
conn = DBConnect.getConnection();
//创建一statement对象
st = conn.createStatement();
// 执行查询操纵,得到结果集
rs = st.executeQuery(sql);
// 判断该登录用户是否存在
while (rs.next()){
// 当前用户存在
userType = rs.getString(1);
} rs.close();
st.close();
conn.close();
} catch (Exception e){
e.printStackTrace();
// System.out.println();
}
return userType;
}
}//使用PreparedStatement测试
public class DBUtil {
// 构造函数
public DBUtil(){ } // 登录验证
public String getUser(String userID,String userPass){
// 声明一字符,用来返回用户类型
String userType = null;
// 声明一查询语句
String sql =
"select USER_ROLE from user_info where USER_ID = ? and USER_PASS = ? ";
// 声明一Connection
Connection conn = null;
// 声明一prestatement
PreparedStatement pstmt = null;
//Statement st = null;
// 声明一resultSet
ResultSet rs = null;
try{
// 建立连接
conn = DBConnect.getConnection();
// 获取一个preparedStatement对象
pstmt = conn.prepareStatement(sql);
// 设定该对象总的参数
pstmt.setString(1,userID);
pstmt.setString(2,userPass);
// 执行查询操纵,得到结果集
rs = pstmt.executeQuery();
// 判断该登录用户是否存在
while (rs.next()){
// 当前用户存在
userType = rs.getString(1);
System.out.println(userType);
} rs.close();
pstmt.close();
//st.close();
conn.close();
} catch (Exception e){
e.printStackTrace();
// System.out.println();
}
return userType;
}
}测试类public class Test {
public static void main(String args[]){
String userType = null;
//使用PreparedStatement测试
DBUtil dbutil = new DBUtil();
//使用Statement测试
//DBUtil1 dbutil = new DBUtil1();
userType = dbutil.getUser("WELL","123456");
System.out.println(userType);
}
}问题:使用Statement可以得到正常结果,但是PreparedStatement得到错误结果。大家看看是PreparedStatement哪个地方有问题!谢谢先
public class DBUtil1 {
// 构造函数
public DBUtil1(){ } // 登录验证
public String getUser(String userID,String userPass){
// 声明一字符,用来返回用户类型
String userType = null;
// 声明一查询语句
String sql = "select USER_ROLE from user_info where USER_ID = '" + userID + "' and USER_PASS = '" + userPass + "'";
// 声明一Connection
Connection conn = null;
// 声明一statement
Statement st = null;
// 声明一resultSet
ResultSet rs = null;
try{
// 建立连接
conn = DBConnect.getConnection();
//创建一statement对象
st = conn.createStatement();
// 执行查询操纵,得到结果集
rs = st.executeQuery(sql);
// 判断该登录用户是否存在
while (rs.next()){
// 当前用户存在
userType = rs.getString(1);
} rs.close();
st.close();
conn.close();
} catch (Exception e){
e.printStackTrace();
// System.out.println();
}
return userType;
}
}//使用PreparedStatement测试
public class DBUtil {
// 构造函数
public DBUtil(){ } // 登录验证
public String getUser(String userID,String userPass){
// 声明一字符,用来返回用户类型
String userType = null;
// 声明一查询语句
String sql =
"select USER_ROLE from user_info where USER_ID = ? and USER_PASS = ? ";
// 声明一Connection
Connection conn = null;
// 声明一prestatement
PreparedStatement pstmt = null;
//Statement st = null;
// 声明一resultSet
ResultSet rs = null;
try{
// 建立连接
conn = DBConnect.getConnection();
// 获取一个preparedStatement对象
pstmt = conn.prepareStatement(sql);
// 设定该对象总的参数
pstmt.setString(1,userID);
pstmt.setString(2,userPass);
// 执行查询操纵,得到结果集
rs = pstmt.executeQuery();
// 判断该登录用户是否存在
while (rs.next()){
// 当前用户存在
userType = rs.getString(1);
System.out.println(userType);
} rs.close();
pstmt.close();
//st.close();
conn.close();
} catch (Exception e){
e.printStackTrace();
// System.out.println();
}
return userType;
}
}测试类public class Test {
public static void main(String args[]){
String userType = null;
//使用PreparedStatement测试
DBUtil dbutil = new DBUtil();
//使用Statement测试
//DBUtil1 dbutil = new DBUtil1();
userType = dbutil.getUser("WELL","123456");
System.out.println(userType);
}
}问题:使用Statement可以得到正常结果,但是PreparedStatement得到错误结果。大家看看是PreparedStatement哪个地方有问题!谢谢先
解决方案 »
- 麻雀虽小,五脏俱全:只为新新手,关于tomcat的环境变量
- struts2标签生成html问题,在线等答案 --菜鸟@江湖救急
- Java代码中怎么使用cookie
- 如何用action实现JSP页面上的功能??急!!!
- 关于生成静态分页问题,望高手进来讨论,并指点一二
- 怎样才能实现照片的连续播放功能 救命啊
- 请给我一个防止刷新的,用图片代替数字的JSP计数器的源代码好吗?50分求
- Could not obtain connection metadata
- jsp中将日文字写到数据库sql server 是正常的没有乱码,但在页面取出时有部门地方出现问号 ??????
- tomcat里如果作将j2ee事务处理?不是基于connect的事务处理?
- java如何操作连接数据库,和操作web中的表格
- vss获取最新版本时提示merge还是leave
应该没有什么问题,如果有也是参数类型不正确
数据库中参数为char型
参数类型为String
应该不会是这个问题吧!
用statement可以得到用户的类型,
而用PreparedStatement的到用户类型为null。这个明显是错误的!
、
感觉是没有赋值成功!
UP!!!!
USER_ROLE from user_info where USER_ID = ? and USER_PASS like ?
数据库中参数为char型
参数类型为String
应该不会是这个问题吧!很明显就是这里出问题了,因为char有空格,lz不妨把sql的那个char的字段先trim后再对比