急急急!!!为什么这条sql语句查不出来数据

feng952409342 2013-05-09 09:57:25
public List<Question> queryQByUCIdAndTypeName(int courseId,String typeName,int userId){
List<Question> list = new ArrayList<Question>();
Question question =null;
try{
//获取连接
conn =DBConn.getConn();
//查询所有试卷信息的sql语句
String sql ="select question_id,question_title,choiceA,choiceB," +
"choiceC,choiceD,answer,mark,course_id,typename " +
"from question where typename=? " +
"and question_id in (select question_id from relation where user_id=? and course_id=?)";
pstm=conn.prepareStatement(sql);
pstm.setString(1,typeName);
pstm.setInt(2,userId);
pstm.setInt(3,courseId);
//获取rs结果集
rs=pstm.executeQuery();
System.out.println("sql cha");
while(rs.next()){
//实例化question对象
question = new Question();
//设置question的属性值
question.setQuestionId(rs.getInt(1));
question.setQuestionTitle(rs.getString(2));
question.setChoiceA(rs.getString(3));
question.setChoiceB(rs.getString(4));
question.setChoiceC(rs.getString(5));
question.setChoiceD(rs.getString(6));
question.setAnswer(rs.getString(7));
question.setMark(rs.getInt(8));
question.setCourseId(rs.getInt(9));
question.setTypeName(rs.getString(10));
list.add(question);
System.out.println("sql cha111");
}

}catch(Exception e){
System.out.println("查询所有试题根据考试参加课程的考试的题目类型查找题目失败!");
e.printStackTrace();
}finally{
this.close();
}
return list;
}
...全文
139 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
happypiggy2010 2013-05-10
  • 打赏
  • 举报
回复
最好是整句sql拿出来分析。
feng952409342 2013-05-10
  • 打赏
  • 举报
回复
用statement对象操作也不能查出数据,为什么呀,求解答(数据库中有很多数据) public List<Question> queryQByUCIdAndTypeName(int courseId,String typeName,int userId){ List<Question> list = new ArrayList<Question>(); Question question =null; try{ //获取连接 conn =DBConn.getConn(); //查询所有试卷信息的sql语句 //String sql ="select question_id,question_title,choiceA,choiceB," + // "choiceC,choiceD,answer,mark,course_id,typename " + // "from question where typename=? " + // "and question_id in (select question_id from relation where user_id=? and course_id=?)"; String sql1="select question_id,question_title,choiceA,choiceB," + "choiceC,choiceD,answer,mark,course_id,typename " + "from question where typename='"+typeName+"'"+ "and question_id in (select question_id from relation where user_id="+userId+" and course_id="+courseId+")"; //pstm=conn.prepareStatement(sql1); //pstm.setString(1,typeName); //pstm.setInt(2,userId); //pstm.setInt(3,courseId); //获取rs结果集 Statement stmt =conn.createStatement(); rs=stmt.executeQuery(sql1); //rs=pstm.executeQuery(); System.out.println("sql cha"); while(rs.next()){ //实例化question对象 question = new Question(); //设置question的属性值 question.setQuestionId(rs.getInt(1)); question.setQuestionTitle(rs.getString(2)); question.setChoiceA(rs.getString(3)); question.setChoiceB(rs.getString(4)); question.setChoiceC(rs.getString(5)); question.setChoiceD(rs.getString(6)); question.setAnswer(rs.getString(7)); question.setMark(rs.getInt(8)); question.setCourseId(rs.getInt(9)); question.setTypeName(rs.getString(10)); list.add(question); System.out.println("sql cha111"); } }catch(Exception e){ System.out.println("查询所有试题根据考试参加课程的考试的题目类型查找题目失败!"); e.printStackTrace(); }finally{ this.close(); } return list; }
feng952409342 2013-05-10
  • 打赏
  • 举报
回复
我把这它改成了Statement操作对象,出问题了!String sql1="select question_id,question_title,choiceA,choiceB," + "choiceC,choiceD,answer,mark,course_id,typename " + "from question where typename='"+typeName+"'"+ "and question_id in(select question_id from relation where user_id="+userId+"+ and course_id="+courseId+")"; //pstm=conn.prepareStatement(sql1); //pstm.setString(1,typeName); //pstm.setInt(2,userId); //pstm.setInt(3,courseId); //获取rs结果集 Statement stmt =conn.createStatement(); rs=stmt.executeQuery(sql1); com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'and course_id=2)' at line 1 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665) at com.mysql.jdbc.Connection.execSQL(Connection.java:3170) at com.mysql.jdbc.Connection.execSQL(Connection.java:3099) at com.mysql.jdbc.Statement.executeQuery(Statement.java:1169)
feng952409342 2013-05-09
  • 打赏
  • 举报
回复
我用的是MySQL5.0我把这条语句的条件控制比方说设置好where条件中问号的值,它有时候可以查有时候不能查到,条件是一样的,不知道为什么会出现这种情况。数据库表中我手动插入了 很多条数据
發糞塗牆 2013-05-09
  • 打赏
  • 举报
回复
通过跟踪先获取到纯sql,然后到查询分析器或者SSMS上面执行一下看看是哪个条件(通常是where条件)导致没有数据,也有可能本身就没有数据

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧