1、设计思想
在DBUtil.java中连接数据库,然后用logonwindow.jsp写添加界面,logon.jsp写执行过程。其中用if语句判断输入内容是否符合要求。
2、源代码
package com.jaovo.msg.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBUtil { public static Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String user = "root"; String password = "root"; String url = "jdbc:mysql://localhost:3306/jaovo_msg"; Connection connection = null; try { connection = DriverManager.getConnection(url, user, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return connection; } }
<%@page import = "com.jaovo.msg.util.DBUtil" %> <%@page import = "java.sql.*" %> <%@page import = "java.sql.PreparedStatement" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <% String classname = request.getParameter("classname"); String teacher = request.getParameter("teacher"); String address = request.getParameter("address"); if(classname == null){ request.setAttribute("result", "请输入课程名称!"); %> <jsp:forward page = "logonwindow.jsp"></jsp:forward> <% } if(teacher.equals("王建民")||teacher.equals("刘立嘉")||teacher.equals("刘丹")|| teacher.equals("王辉")||teacher.equals("杨子光")){ } else{ request.setAttribute("result", "该教师不存在!"); %> <jsp:forward page = "logonwindow.jsp"></jsp:forward> <% } if(address.startsWith("基教") || address.startsWith("一教") || address.startsWith("二教") || address.startsWith("三教") ){ } else{ request.setAttribute("result", "上课地址错误!"); %> <jsp:forward page = "logonwindow.jsp"></jsp:forward> <% } Connection connection = DBUtil.getConnection(); //准备sql语句 String sql = "select count(*) from kechengbiao where classname = ?"; //创建语句传输对象 PreparedStatement preparedStatement = null; ResultSet resultSet = null; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, classname); //接收结果集 resultSet = preparedStatement.executeQuery(); //遍历结果集 while(resultSet.next()) { if (resultSet.getInt(1) > 0) { request.setAttribute("result", "该课程已添加!"); %> <jsp:forward page = "logonwindow.jsp"></jsp:forward> <% } } sql = "INSERT INTO kechengbiao(classname,teacher,address) VALUES (?,?,?) "; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, classname); preparedStatement.setString(2, teacher); preparedStatement.setString(3, address); preparedStatement.execute(); request.setAttribute("result", "添加完成"); %> <jsp:forward page = "logonwindow.jsp"></jsp:forward> </html>
<%@page import = "com.jaovo.msg.util.DBUtil" %> <%@page import = "java.sql.*" %> <%@page import = "java.sql.PreparedStatement" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <% String classname = request.getParameter("classname"); String teacher = request.getParameter("teacher"); String address = request.getParameter("address"); if(classname == null){ request.setAttribute("result", "请输入课程名称!"); %> <jsp:forward page = "logonwindow.jsp"></jsp:forward> <% } if(teacher.equals("王建民")||teacher.equals("刘立嘉")||teacher.equals("刘丹")|| teacher.equals("王辉")||teacher.equals("杨子光")){ } else{ request.setAttribute("result", "该教师不存在!"); %> <jsp:forward page = "logonwindow.jsp"></jsp:forward> <% } if(address.startsWith("基教") || address.startsWith("一教") || address.startsWith("二教") || address.startsWith("三教") ){ } else{ request.setAttribute("result", "上课地址错误!"); %> <jsp:forward page = "logonwindow.jsp"></jsp:forward> <% } Connection connection = DBUtil.getConnection(); //准备sql语句 String sql = "select count(*) from kechengbiao where classname = ?"; //创建语句传输对象 PreparedStatement preparedStatement = null; ResultSet resultSet = null; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, classname); //接收结果集 resultSet = preparedStatement.executeQuery(); //遍历结果集 while(resultSet.next()) { if (resultSet.getInt(1) > 0) { request.setAttribute("result", "该课程已添加!"); %> <jsp:forward page = "logonwindow.jsp"></jsp:forward> <% } } sql = "INSERT INTO kechengbiao(classname,teacher,address) VALUES (?,?,?) "; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, classname); preparedStatement.setString(2, teacher); preparedStatement.setString(3, address); preparedStatement.execute(); request.setAttribute("result", "添加完成"); %> <jsp:forward page = "logonwindow.jsp"></jsp:forward> </html>
3、运行结果截图
4、