课堂练习-增加信息
Posted ╄冷丶夜♂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了课堂练习-增加信息相关的知识,希望对你有一定的参考价值。
程序设计思想:
1.创建程序所需要用到的一些类,根据功能分包存放:
其中ClassInfo定义三个私有属性:name、teacher、place,并对它们创建get与set方法
2.在WebContent目录下建立addclass.jsp文件,在此jsp文件中添加表单,通过表单将输入的信息提交至本页面,经过本Jsp页面的处理将信息添加至数据库,并通过javascript脚本弹出提示对话框。
源程序代码如下:
DBUtil.java:
1 package com.jaovo.msg.Util; 2 import java.sql.Connection; 3 import java.sql.DriverManager; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 8 public class DBUtil { 9 10 public static Connection getConnection() { 11 try { 12 Class.forName("com.mysql.jdbc.Driver").newInstance(); 13 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { 14 // TODO Auto-generated catch block 15 e.printStackTrace(); 16 } 17 String user = "root"; 18 String password = "root"; 19 String url = "jdbc:mysql://localhost:3306/jaovo_msg"; 20 Connection connection = null; 21 try { 22 connection = DriverManager.getConnection(url,user,password); 23 } catch (SQLException e) { 24 // TODO Auto-generated catch block 25 e.printStackTrace(); 26 } 27 return connection; 28 } 29 30 public static void close(Connection connection ) { 31 try { 32 if (connection != null) { 33 connection.close(); 34 } 35 36 } catch (SQLException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } 40 } 41 public static void close(PreparedStatement preparedStatement ) { 42 try { 43 if (preparedStatement != null) { 44 preparedStatement.close(); 45 } 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 58 } catch (SQLException e) { 59 // TODO Auto-generated catch block 60 e.printStackTrace(); 61 } 62 } 63 }
ClassInfo.java
1 package com.jaovo.msg.model; 2 3 public class ClassInfo { 4 private String name; 5 private String teacher; 6 private String place; 7 8 public ClassInfo(String name, String teacher, String place) { 9 this.name = name; 10 this.teacher = teacher; 11 this.place = place; 12 } 13 public String getName() { 14 return name; 15 } 16 public void setName(String name) { 17 this.name = name; 18 } 19 public String getTeacher() { 20 return teacher; 21 } 22 public void setTeacher(String teacher) { 23 this.teacher = teacher; 24 } 25 public String getPlace() { 26 return place; 27 } 28 public void setPlace(String place) { 29 this.place = place; 30 } 31 }
IClassDao.java:
1 package com.jaovo.msg.dao; 2 3 import com.jaovo.msg.model.ClassInfo; 4 5 public interface IClassDao { 6 public void add(ClassInfo clas); 7 }
ClassDaoImpl.java:
1 package com.jaovo.msg.dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.SQLException; 6 7 import com.jaovo.msg.Util.DBUtil; 8 import com.jaovo.msg.model.ClassInfo; 9 10 public class ClassDaoImpl implements IClassDao{ 11 12 @Override 13 public void add(ClassInfo clas) { 14 // TODO Auto-generated method stub 15 Connection con = DBUtil.getConnection(); 16 String sql = "insert into class_info(classname,teacher,place) value (?,?,?)"; 17 PreparedStatement ps = null; 18 try { 19 ps = con.prepareStatement(sql); 20 ps.setString(1, clas.getName()); 21 ps.setString(2, clas.getTeacher()); 22 ps.setString(3, clas.getPlace()); 23 ps.executeUpdate(); 24 } catch (SQLException e) { 25 // TODO Auto-generated catch block 26 e.printStackTrace(); 27 }finally{ 28 DBUtil.close(ps); 29 DBUtil.close(con); 30 } 31 } 32 33 }
运行结果截图:
以上是关于课堂练习-增加信息的主要内容,如果未能解决你的问题,请参考以下文章