编程语言
首页 > 编程语言> > Java学习二十三--新课程添加

Java学习二十三--新课程添加

作者:互联网

这次的任务要求实现数据库连接和界面操作

一、需求(思路)

  1、创建课程类,实现get和set方法

  2、创建辅助类,连接数据库。

  3、创建插入类,将数据添加至数据库

  4、创建前端.jsp文件

  5、创建后端.jsp文件

二、代码

  1、

 1 package AddCurriculum;
 2 //课程类
 3 public class HaveClass {
 4     private String classname;//课程姓名
 5     private String teachername;//教师名称
 6     private String place;//上课地点
 7     
 8     public String getclassname() {
 9         return classname;
10     }
11     public void setclassname(String classname) {
12         this.classname=classname;
13     }
14     
15     public String getteachername() {
16         return teachername;
17     }
18     public void setteachername(String teachername) {
19         this.teachername=teachername;
20     }
21     
22     public String getplace() {
23         return place;
24     }
25     public void setplace(String place) {
26         this.place=place;
27     }
28     
29     
30     
31     
32     
33     
34 }

  2、

 1 package com.msg;
 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 
10 public class DButil {
11     public static Connection getConnection() {
12         try {
13             Class.forName("com.mysql.jdbc.Driver");
14         } catch (ClassNotFoundException e) {
15             // TODO Auto-generated catch block
16             e.printStackTrace();
17         }
18         String user="root";
19         String passwd="passwd";//此处为mysql数据库密码
20         String url="jdbc:mysql://localhost:3306/curriculum";//curriculum是数据库的名称
21         Connection connection=null;
22         try {
23             connection=DriverManager.getConnection(url,user,passwd);
24         } catch (SQLException e) {
25             // TODO Auto-generated catch block
26             e.printStackTrace();
27         }
28         return connection;
29     }
30     
31     public static void close(Connection connection) {
32         try {
33             if(connection!=null) {
34                 connection.close();
35             }
36         }catch(SQLException e) {
37             e.printStackTrace();
38         }
39     }
40     
41     public static void close(PreparedStatement preparedStatement) {
42         try {
43             if(preparedStatement!=null) {
44                 preparedStatement.close();
45             }
46         }catch(SQLException e) {
47             e.printStackTrace();
48         }
49     }
50     
51     public static void close(ResultSet resultSet) {
52         try {
53             if(resultSet!=null) {
54                 resultSet.close();
55             }
56         }catch(SQLException e) {
57             e.printStackTrace();
58         }
59     }
60 }

  3、

 1 package Insert;
 2 //将数据添加至数据库中
 3 import java.sql.*;
 4 
 5 import com.msg.DButil;
 6 
 7 import AddCurriculum.HaveClass;
 8 
 9 public class ClassInsert {
10     public void add(HaveClass claz)  {
11         
12         
13         //获取连接对象
14         Connection connection=DButil.getConnection();
15         
16         //准备sql语句
17         String sql="insert into curr(classname,teachername,place) values(?,?,?)";
18         PreparedStatement preparedStatement=null;
19         try {
20             //创建语句传输对象
21             preparedStatement=connection.prepareStatement(sql);
22             preparedStatement=connection.prepareStatement(sql);
23             preparedStatement=connection.prepareStatement(sql);
24             preparedStatement.setString(1, claz.getclassname());
25             preparedStatement.setString(2,claz.getteachername());
26             preparedStatement.setString(3, claz.getplace());
27             
28             preparedStatement.executeUpdate();
29         }catch(SQLException e) {
30             e.printStackTrace();
31         }finally {
32             //关闭资源
33             DButil.close(preparedStatement);
34             DButil.close(connection);
35         }
36     }
37 }

  4、

 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 <meta charset="UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <form action="add.jsp" method="get">
11         <table align="center" border="1" width="500">
12             <tr>
13                 <td>课程名称:</td>
14                 <td>
15                     <input type="text" name="classname"/>
16                 </td>
17             </tr>
18                 <tr>
19                 <td>任课教师:</td>
20                 <td>
21                     <input type="text" name="teachername"/>
22                 </td>
23             </tr>
24             <tr>
25                 <td>上课地点:</td>
26                 <td>
27                     <input type="text" name="place"/>
28                 </td>
29             </tr>
30             <tr align="center">
31                 <td colspan="2">
32                     <input type="submit" value="提交"/>
33                     <input type="reset" value="重复"/>
34                 </td>
35             </tr>
36         </table>
37     </form>
38 </body>
39 </html>

  5、

 1 <%@page import="com.msg.ClassException"%>
 2 <%@page import="Insert.ClassInsert"%>
 3 <%@page import="AddCurriculum.HaveClass"%>
 4 <%@ page language="java" contentType="text/html; charset=UTF-8"
 5     pageEncoding="UTF-8"%>
 6  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 7 <html>
 8 <%
 9     //接收客户端传递过来的参数
10 
11     String teachername = request.getParameter("teachername");
12     String classname = request.getParameter("classname");
13     String place = request.getParameter("place");
14  
15         HaveClass haveclass = new HaveClass();
16         haveclass.setclassname(classname);
17         haveclass.setteachername(teachername);
18         haveclass.setplace(place);
19         
20         ClassInsert classDao = new ClassInsert();
21         classDao.add(haveclass);
22   
23     
24 
25 
26 %>
27         
28 
29 </html>

三、运行截图

 

 

 

 此次的程序做的其实不够完善,我会接着改进,并将改进代码发表

标签:新课程,preparedStatement,Java,String,--,connection,sql,close,public
来源: https://www.cnblogs.com/jzz-111jy/p/15415430.html