课堂练习:增加信息
一,程序设计思想
首先使用jsp设计添加界面,该界面应包含一个提交按钮和三个输入框
其次使用SQL语句在后台建立对应的表结构以存储课程信息,该表应至少含有课程信息,教师信息,上课地点三列,为以后着想,可以增加id列。
然后新建add.jsp文件用以接收用户传来的参数,并在数据库中插入
为判断教师姓名,上课地点,在添加界面,当用户提交的教师姓名,上课地点不符合要求,则跳转到报错页面
二,源程序代码
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <title>信息添加页面</title> 7 </head> 8 <body> 9 <form action="add.jsp" method="get"> 10 <table align="center" border="1" width="500"> 11 <tr> 12 <td> 课程名称 </td> 13 <td> 14 <input type="text" name="classname" style="width: 300px; "/> 15 </td> 16 </tr> 17 <tr> 18 <td>课程教师</td> 19 <td> 20 <input type="text" name="teachername" style="width: 300px; "/> 21 </td> 22 </tr> 23 <tr> 24 <td>上课教室</td> 25 <td> 26 <input type="text" name="place" style="width: 300px; "/> 27 </td> 28 </tr> 29 <tr align="center"> 30 <td colspan="2"> 31 <input type="submit" value="保存" style="color: Blue; background-color: Lime; height: 70px; width: 200px"/> 32 </td> 33 </tr> 34 </table> 35 </form> 36 </body> 37 </html>
1 <%@page import="com.jaovo.msg.teach.shujuku"%> 2 <%@page import="com.jaovo.msg.teach.student"%> 3 <%@ page language="java" contentType="text/html; charset=UTF-8" 4 pageEncoding="UTF-8"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <% 8 //接收客户端传递过来的参数 9 String classname = request.getParameter("classname"); 10 String teachername = request.getParameter("teachername"); 11 String place = request.getParameter("place"); 12 student st=new student(); 13 System.out.println(classname); 14 st.setClassname(classname); 15 st.setTeachername(teachername); 16 st.setPlace(place); 17 shujuku s = new shujuku(); 18 try{ 19 s.gets(st); 20 } 21 catch(NullPointerException q) 22 { 23 q.printStackTrace(); 24 } 25 26 %> 27 </html>
package com.jaovo.msg.teach; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.jaovo.msg.Util.DBUtil; public class shujuku { public void gets(student st) { //获得链接对象 ResultSet resultSet=null; PreparedStatement preparedStatement = null; Connection connection = DBUtil.getConnection(); //准备sql语句 String sql = "insert into myclass(classname,teachername,place) values(?,?,?)"; try { preparedStatement = connection.prepareStatement(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { preparedStatement.setString(1, st.getClassname()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { preparedStatement.setString(2, st.getTeachername()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { preparedStatement.setString(3, st.getPlace()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //关闭资源 DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } }
1 package com.jaovo.msg.teach; 2 3 public class student { 4 5 private int id; 6 public int getId() { 7 return id; 8 } 9 public void setId(int id) { 10 this.id = id; 11 } 12 private String teachername; 13 private String classname; 14 private String place; 15 public String getTeachername() { 16 return teachername; 17 } 18 public void setTeachername(String teachername) { 19 this.teachername = teachername; 20 } 21 public String getClassname() { 22 return classname; 23 } 24 public void setClassname(String classname) { 25 this.classname = classname; 26 } 27 public String getPlace() { 28 return place; 29 } 30 public void setPlace(String place) { 31 this.place = place; 32 } 33 }
1 package com.jaovo.msg.Util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 public class DBUtil { 10 public static Connection getConnection() { 11 try { 12 //1 加载驱动 13 Class.forName("com.mysql.jdbc.Driver").newInstance(); 14 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { 15 // TODO Auto-generated catch block 16 e.printStackTrace(); 17 } 18 String username = "root"; 19 String password = "root"; 20 String url = "jdbc:mysql://localhost:3306/class"; 21 Connection connection = null; 22 try { 23 //2 创建链接对象connection 24 connection = DriverManager.getConnection(url,username,password); 25 } catch (SQLException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } 29 return connection; 30 } 31 //关闭资源的方法 32 public static void close(Connection connection ) { 33 try { 34 if (connection != null) { 35 connection.close(); 36 } 37 } catch (SQLException e) { 38 // TODO Auto-generated catch block 39 e.printStackTrace(); 40 } 41 } 42 public static void close(PreparedStatement preparedStatement ) { 43 try { 44 if (preparedStatement != null) { 45 preparedStatement.close(); 46 } 47 } catch (SQLException e) { 48 // TODO Auto-generated catch block 49 e.printStackTrace(); 50 } 51 } 52 public static void close(ResultSet resultSet ) { 53 try { 54 if (resultSet != null) { 55 resultSet.close(); 56 } 57 } catch (SQLException e) { 58 // TODO Auto-generated catch block 59 e.printStackTrace(); 60 } 61 } 62 }
1 package com.jaovo.msg.Util; 2 3 public class UserException extends RuntimeException{ 4 5 private static final long serialVersionUID = 1L; 6 7 public UserException() { 8 super(); 9 // TODO Auto-generated constructor stub 10 } 11 12 public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { 13 super(message, cause, enableSuppression, writableStackTrace); 14 // TODO Auto-generated constructor stub 15 } 16 17 public UserException(String message, Throwable cause) { 18 super(message, cause); 19 // TODO Auto-generated constructor stub 20 } 21 22 public UserException(String message) { 23 super(message); 24 // TODO Auto-generated constructor stub 25 } 26 27 public UserException(Throwable cause) { 28 super(cause); 29 // TODO Auto-generated constructor stub 30 } 31 32 }
SQL语句
1 create database class; 2 use class; 3 GRANT ALL ON class.* to "jaovo"@"localhost" IDENTIFIED BY "root"; 4 create table myclass( 5 id int(10) primary key auto_increment, 6 classname varchar(255), 7 teachername varchar(255), 8 place varchar(255)) 11 );
三,运行效果截图
下面是在Navicat里的结果
项目计划日志
估计需要花费两个小时
实际花费三个小时
时间记录日志
缺陷记录日志
MySQL数据库中文编码问题
提交后无提示语句