易課程course
『壹』 jsp代碼注釋 本人菜鳥 希望詳細些 通俗易懂些
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> //這是JSP頁面的頭,裡麵包含了該頁的解釋格式,編碼格式,還有使用的語言,import表示導入java.sql包。
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<html> //以下為HTML語言
<head>
<title>選課系統</title>
</head>
<body> //小腳本開始
<%
String courseNo=request.getParameter("courseNo");從request對象中獲取"courseNo"的值,賦給聲明的名為courseNo的變數
Class.forName("com.mysql.jdbc.Driver"); //載入驅動,該處為mysql驅動
String url="jdbc:mysql://localhost:3306/coursesystem?useUnicode=true&characterEncoding=gb2312";//聲明一個String類型變數,裡面保存的是url
Connection conn=DriverManager.getConnection(url,"root","student"); //這是創建連接,此處皆為jdbc知識,根據各種資料庫,會略有不同
Statement stmt=conn.createStatement(); //創建陳述
try{
conn.setAutoCommit(false); //設置連接的自動提交為false,主要用於事物中,取消自動提交可以防止提交時因為不可抗拒因素造成的錯誤。因為下面有兩句sql,可以防止在執行一句sql後出現問題,第二句沒有執行,就會出現錯誤
String sql1="delete from course where Cno='"+ courseNo +"'"; //創建sql語句。
stmt.executeUpdate(sql1); //執行sql語句
String sql2="delete from SC where Cno='"+ courseNo +"'"; //創建sql語句
stmt.executeUpdate(sql2); //執行sql語句
conn.commit(); //這里是手動提交
conn.setAutoCommit(true); //然後再把自動提交改回去
}catch (SQLException e) { //這些是異常處理
e.printStackTrace();
try{
conn.rollback(); //這是事務的回滾,出現問題時會恢復到修改前的狀態
}catch(Exception ex) {
ex.printStackTrace();
}
}
%> //小腳本結束
刪除課程信息成功!
<%stmt.close(); //小腳本開始,關閉陳述和連接
conn.close();%> //小腳本結束
</body>
</html>
第二篇scQuery
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<title>選課系統</title>
</head>
<body>
<%String studentNo=request.getParameter("studentNo"); //從request對象中取"studentNo"的值,賦給studentNo變數
if (studentNo==null)studentNo=""; //判斷如果studentNo為null,設置studentNo為空
Class.forName("com.mysql.jdbc.Driver"); //載入驅動
String url="jdbc:mysql://localhost:3306/courseSystem?user=root&password=student"
+"&useUnicode=true&characterEncoding=gb2312"; //url
Connection conn=DriverManager.getConnection(url); //創建連接
Statement stmt=conn.createStatement(); //創建陳述
String sql="select sc.Sno,Sname,sc.Cno,Cname,Grade"
+" from student,course,sc where student.Sno=sc.Sno"
+" and course.Cno=sc.Cno "; //創建sql語句
if(! studentNo.equals("")){ //判斷studentNo不等於空字元串
sql=sql+" and sc.Sno='"+studentNo+"'"; //如果不為空,在sql語句後面拼接上" and sc.Sno='"+studentNo+"'";
};
sql=sql+"order by sc.Cno"; //拼接sql
ResultSet rs=stmt.executeQuery(sql);%> //執行sql,把結果放入結果集當中
<center><h2>選課及成績情況</h2></center>
<table width=500 align=center border=1>
<tr align=center>
<td><b>姓名</td>
<td><b>課程名</td>
<td><b>成績</td>
</tr>
<%while(rs.next()){%> //結果集游標向下一行,就是判斷如果結果集是否有值
<tr align=center>
<td><%=rs.getString("Sname")%></td> //表達式從結果集中取叫"Sname"的值
<td><%=rs.getString("Cname")%></td> //表達式從結果集中取叫"Cname"的值
<td><%=rs.getInt("Grade")%></td> //表達式從結果集中取叫"Grade"的值
</tr>
<% }%>
</table>
<%
stmt.close(); //關閉陳述
conn.close(); //關閉連接
%>
</body>
</html>