各位大侠,我是大二的学生,正在做课程设计,关于教师任课信息的,下面是我想实现查看教师任课信息的servlet部分代码
package cn.com.jobedu.my_case;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
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;
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 {
Connection conn = ds.getConnection();
String sql = "select * from CourseInfo,TeacherInfo,CaseInfo where ";
} catch (SQLException e) {
e.printStackTrace();
}
}
}
我在sql server 2005中建了几个表,分别是课程信息表CourseInfo(Coid,Coname)、教师信息表TeacherInfo(Tid,Tname,)、任课信息表CaseInfo(Coid,Tid,Catoge,Catime),
现在我想通过从jsp页面传过来的两个参数:Coname,Tname读出对应的CaseInfo表中的任课信息,sql语句可以通过连接查询实现,但是怎样跟参数相关联呢?这里有2个参数都要用上才查的到,不像一个参数的sql语句可以这样写:String sql="select * from CourseInfo,TeacherInfo,CaseInfo where 、、、"+id(假设传过来的某个参数是id)
不好意思说了这么多,我其实最想问的就是前面红色字String sql那一行该怎样写
小弟是刚刚注册的,所以没什么分给,希望各位大侠不要嫌弃,帮我解答一下,谢谢!
------解决方案--------------------
String sql = "select * from CourseInfo,TeacherInfo,CaseInfo where 1=1";
if(Coname有值){
sql = sql + " and conname = '"+conname+"'"
}
Tname 同理,
楼主是不是这个意思?