第一個:“insert.jsp”檔案
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加課程</title>
</head>
<body>
<form action="insertjudge.jsp">
<table>
<h2>請輸入要添加的課程資訊:</h2>
<tr>
<td>課程號:</td>
<td><input type="text" name="cno"></td>
</tr>
<tr>
<td>課程名:</td>
<td><input type="text" name="cname"></td>
</tr>
<tr>
<td>開課學期:</td>
<td><input type="text" name="cterm"></td>
</tr>
<tr>
<td>開課專業:</td>
<td><input type="text" name="cmajor"></td>
</tr>
<tr>
<td>學時:</td>
<td><input type="text" name="chour"></td>
</tr>
<tr>
<td>學分:</td>
<td><input type="text" name="ccredit"></td>
</tr>
<tr>
<td>任課教師:</td>
<td><input type="text" name="tno"></td>
</tr>
<tr>
<td>課程介紹:</td>
<td><textarea name="cIntro" rows="3" cols="20"></textarea></td>
</tr>
<tr><td> </td><td> </td></tr>
<tr align="center">
<td><input type="submit" value="https://bbs.csdn.net/topics/添加" name="submit"></td>
<td><input type="reset" value="https://bbs.csdn.net/topics/重置" name="reset"></td>
</tr>
</table>
</form>
</body>
</html>
第二個(跳轉的頁面):“insertjudge.jsp”
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加資料判斷</title>
</head>
<body>
<%
request.setCharacterEncoding("gb2312");
String cno=request.getParameter("cno");
String cname=request.getParameter("cname");
String cterm=request.getParameter("cterm");
String cmajor=request.getParameter("cmajor");
String chour=request.getParameter("chour");
String ccredit=request.getParameter("ccredit");
String tno=request.getParameter("tnum");
String cintro=request.getParameter("cintro");
String url = "jdbc:mysql://localhost:3306/scms?"+ "useSSL=false&serverTimezone=CST&characterEncoding=utf-8"; //連接串
String user="root";
String password="2001511ty";
Class.forName("com.mysql.cj.jdbc.Driver"); //加載驅動
Connection conn=DriverManager.getConnection(url,user,password); //創建與資料庫的連接
Statement stmt=conn.createStatement(); //執行sql陳述句
String sql="INSERT INTO course VALUES('"+cno+"','"+cname+"','"+cterm+"','"+cmajor+"','"+chour+"','"+ccredit+"','"+tno+"','"+cintro+"')";
int i=stmt.executeUpdate(sql);
if(i==1){
%>
<h2>記錄添加成功</h2>
<%}
else{
%>
<h2>記錄添加失敗!</h2>
<%}
stmt.close();
conn.close();
%>
</body>
</html>
第二個檔案的第28行有錯誤,不知道錯在哪里了:

這個是資料庫的表:
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/284650.html
標籤:JavaScript
