<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>用户登录界面</title> </head> <body> <div id="container"> <form action="putin.jsp" method="post" onSubmit="return check(this)"> <table bgcolor="yellow" border="5" cellpadding="5" cellspacing="20" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="320" > <tr> <th colspan=2>信息保存</th> </tr> <tr> <td align="center" style="color: orangered">课程名称 : </td> <td><input type="text" style="background-color: cornsilk" name="tclassname"/></td> </tr> <tr> <td align="center" style="color: orangered">任课教师 : </td> <td><input type="text" style="background-color: cornsilk" name="tteacher"/></td> </tr> <tr> <td align="center" style="color: orangered">上课地点 : </td> <td><input type="text" style="background-color: cornsilk" name="tplace"/></td> </tr> <%-- <tr> <th colspan=2><img src="yanzherngm.jsp"></th> </tr> --%> <tr align="center"> <td colspan="5"> <input type="submit" style="background-color: aqua" value="保存" /> </td> </tr> </table> </form> </div> </body> </html>
这是初次链接数据库操作里的登录界面,其需要输入的信息有三个,课程名称,任课教师和上课地点,分别是tclassname,tteacher,tplace。
<%@ page import="java.sql.Connection" %> <%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.Statement" %> <%@ page import="java.sql.PreparedStatement" %> <%@ page import="java.sql.*" %> <%@ page import="java.util.ArrayList" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String hey="录入信息有问题,请重新录入"; String tclassname=request.getParameter("tclassname"); String tteacher=request.getParameter("tteacher"); String tplace=request.getParameter("tplace"); try { //1.注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); // SQL语句,使用我自己的test数据库下的 boss 表 String sqlsearch="select count(*) as ct from user where 课程名称= '"+tclassname+"'"; ResultSet re = statement.executeQuery(sqlsearch); re.next(); int count = re.getInt(1); if(count==0&&(tplace.indexOf("一教")!=-1||tplace.indexOf("二教")!=-1||tplace.indexOf("三教")!=-1||tplace.indexOf("基教")!=-1)) { hey = "保存成功"; String sql = "insert into user(课程名称,任课教师,上课地点) value (?,?,?)"; PreparedStatement pstmt; pstmt = connection.prepareStatement(sql); pstmt.setString(1, tclassname); pstmt.setString(2, tteacher); pstmt.setString(3, tplace); pstmt.executeUpdate(); connection.close(); } request.setAttribute("hey",hey); } catch (SQLException e) { } //ResultSet resultSet = statement.executeQuery(sql); //resultSet.close(); //statement.executeUpdate("delete from user where name='n'"); %> <html> <body> <div style="text-align: center" class="container text-center"> <h2 class="form-signin-heading" style="color: brown"> <font size=5> <strong> ${hey} </strong> </font> </h2> <form action="index.jsp"> <input type="submit" value="返回" style="color: darkblue;font-size:35px;background:khaki;border-radius: 15px" > </form> </div> </body> </html>
然后在这段代码里将输入的内容存入sql数据库下的表里,并对课程地点进行判断,若课程名称存在重复或地点里没有存在基教,一教二教三教等地点的话则认为输入有错误,需重新输入,反之则将内容存入自己的表中。
具体表如下:
标签:10,地点,课程名称,存入,任课教师,周一,输入 From: https://www.cnblogs.com/jiajiayu/p/17753253.html