今天是十月二十三号……突然就被要求写JAVAweb连接数据库……
在导入各种JAR、JDBC之后总算是写出了可以储存进数据库并且读取的选课系统
修改后的选课申请提交代码如下:
<%@ page language="java" import="java.util.*" import="java.text.*" import ="java.sql.*" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <title>上课计划</title> </head> <body style="text-align:center;"> <br><br><br> <pre> <% Class.forName("com.mysql.cj.jdbc.Driver"); Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?serverTimezone=UTC","root","260702266"); Statement state=connect.createStatement(); ResultSet rs = state.executeQuery("select count(*) from class"); rs.next(); int result = rs.getInt(1); int one= Integer.parseInt(request.getParameter("degree")); String str=(String)session.getAttribute("account"); switch(one){ case 0: state.executeUpdate("INSERT INTO class2 VALUES ('C++','刘立嘉','一教','"+str+"')"); break; case 1: state.executeUpdate("INSERT INTO class2 VALUES ('JAVA','王建民','基教','"+str+"')"); break; case 2: state.executeUpdate("INSERT INTO class2 VALUES ('数据结构','刘丹','三教','"+str+"')"); break; } out.print("选课成功!"); %> <form name="form3" action="Next1.jsp" method="post"> <input type="submit" value="返回主页面"> </form> </pre> </body> </html>
哎呀……这要求不得不说实在是有点突然……
好,今天就干了这些,休息去咯!