MajorCourseAddSave.jsp
上传用户:jhoneliu
上传日期:2022-07-31
资源大小:950k
文件大小:1k
- <%@page contentType="text/html; charset=gb2312" language="java" import="java.sql.*"%>
- <%
- String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; //驱动程序,见第十四章
- String url = "jdbc:odbc:stud"; //数据库连接语句,见第十四章
- String userID = "sa", pwd = "", sql = "";
- try {
- Class.forName(driver); //调入JDBC-ODBC桥的驱动程序
- Connection conn = DriverManager.getConnection(url, userID, pwd); //建立与数据的连接
- //request对象见第四章
- request.setCharacterEncoding("gb2312"); //乱码转换成中文
- Statement stmt = conn.createStatement(); //建立Statement的实例,相当于创建一个查询分析器的运行环境
- sql = "select * from majorcourse where MajorID='" + request.getParameter("MajorID") + "' and CourseID='" + request.getParameter("CourseID") + "'";
- ResultSet rs = stmt.executeQuery(sql);
- if (!rs.next()) {
- rs.close();
- sql = "insert majorcourse values('" + request.getParameter("MajorID") + "','";
- sql = sql + request.getParameter("CourseID") + "')";
- if (stmt.executeUpdate(sql) > 0) {
- response.sendRedirect("MajorCourseList.jsp"); //response对象见第四章,连接到memCenter.jsp页面
- }
- }
- else {
- %>
- <script language="javascript">
- alert("该专业的该门课程已经存在,请选择其它课程");
- window.history.go(-1);
- </script>
- <%
- }
- }
- catch (Exception e) {
- System.out.println(e.getMessage());
- }
- %>