当前位置: 代码迷 >> Java Web开发 >> 参数跟sql语句,该如何处理
  详细解决方案

参数跟sql语句,该如何处理

热度:9591   发布时间:2013-02-25 21:20:19.0
参数跟sql语句
各位大侠,我是大二的学生,正在做课程设计,关于教师任课信息的,下面是我想实现查看教师任课信息的servlet部分代码

package cn.com.jobedu.my_case;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

public class CaseServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
  doPost(request,response);

}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");
String Coname = request.getParameter("Coname");
String Tname = request.getParameter("Tname");
DataSource ds = null;

try {
// 通过在context.xml文件,设定的数据源对象的名字,获取数据源对象
Context context = new InitialContext();
ds = (DataSource) context.lookup("java:comp/env/jdbc/case");
} catch (Exception e) {
System.out.println("获取数据源时出错");
}

try {

String sql = "select Coname,Tname,Clname,Catoge,Catime from CourseInfo,TeacherInfo,CaseInfo where CaseInfo.Clid=ClassInfo.Clid and CaseInfo.Coid=CourseInfo.Coid and CaseInfo.Tid=TeacherInfo.Tid and Coname='Coname' and Tname= 'Tname'"; QueryRunner qr=new QueryRunner(ds);
List list=(List)qr.query(sql,new BeanListHandler(CaseInfo.class));

} catch (SQLException e) {

e.printStackTrace();
}



}

}
运行时myeclipse中显示出错:
java.sql.SQLException: 无法绑定由多个部分组成的标识符 "ClassInfo.Clid"。 Query: select Coname,Tname,Clname,Catoge,Catime from CourseInfo,TeacherInfo,CaseInfo where CaseInfo.Clid=ClassInfo.Clid and CaseInfo.Coid=CourseInfo.Coid and CaseInfo.Tid=TeacherInfo.Tid and Coname='Coname' and Tname= 'Tname' Parameters: []

就是sql语句那里出错,求各位大侠帮忙看看,我连接的数据库是sql server 2005

------解决方案--------------------------------------------------------
1)select Coname,Tname,Clname,Catoge,Catime 最好加上表别名
2)Coname,Tname,Clname,Catoge,Catime。。。 和CaseInfo.java类中的属性一致。
------解决方案--------------------------------------------------------
把四个表贴出来,不知道你要查的列到底属于哪个表
1:ClassInfo.Clid 是你没有查这个表
2:select Coname,Tname,Clname应该写成:select coi.Coname,tei.Tname .. from CourseInfo coi,TeacherInfo tei ...
------解决方案--------------------------------------------------------
SQL code
String sql = "selectCourse.Coname,Teacher.Tname,Class.Clname,Case.Catoge,Case.Catime from CourseInfo Course,TeacherInfo Teacher,ClassInfo Class,CaseInfo Casewhere Case.Clid=Class.Clid and Case.Coid=Course.Coid and Case.Tid=Teacher.Tid;
------解决方案--------------------------------------------------------
你的错误是不是在这里哦
IndexOutOfBoundsException: Index: 0, Size: 0
  相关解决方案