04课堂练习-增加信息
in 归档 with 0 comment

04课堂练习-增加信息

in 归档 with 0 comment

04课堂练习
最后由@chen_修改于2017-11-28 17:53


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>添加</title>
</head>
<body>
    <form action="addcheck.jsp" method="get">
    <table align="center" border="1" width="500">
        <tr>
            <td>课程名称:</td>
            <td>
            <input type="text" name="name"/>
            </td>
        </tr> 
        <tr>
            <td>上课地点:</td>
            <td>
            <input type="text" name="location">
            </td>
        </tr>
        <tr>
            <td>老师姓名:</td>
            <td>
            <input type="text" name="teacher">
            </td>
        </tr>
        <tr align="center">
            <td colspan="2">
            <input type="submit" value="增加"  />
        </td>
        
        </tr>
</table>
</form>


</body>
</html><%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>添加</title>
</head>
<body>
    <form action="addcheck.jsp" method="get">
    <table align="center" border="1" width="500">
        <tr>
            <td>课程名称:</td>
            <td>
            <input type="text" name="name"/>
            </td>
        </tr> 
        <tr>
            <td>上课地点:</td>
            <td>
            <input type="text" name="location">
            </td>
        </tr>
        <tr>
            <td>老师姓名:</td>
            <td>
            <input type="text" name="teacher">
            </td>
        </tr>
        <tr align="center">
            <td colspan="2">
            <input type="submit" value="增加"  />
        </td>
        
        </tr>
</table>
</form>


</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="P.User"%>
<%@page import="P.UserUtil"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<% 
    String name=request.getParameter("name");
    String location=request.getParameter("location");
    String teacher=request.getParameter("teacher");
    out.print(teacher);

    if(name == null || "".equals(name.trim())){
        request.setAttribute("error", "用户名不能为空");
        %>
        
<%     }
    else{
%>

<% 
    User u=new User();
    u.name=name;
    u.location=location;
    u.teacher=teacher;
    if(UserUtil.check(u)){
        UserUtil.add(u);
        %>    
        <script type="text/javascript" language="javascript">
        alert("保存成功");
    </script>    
<%    
    }else
    {
%>    
        <script type="text/javascript" language="javascript">
        alert("输入有误");
        window.document.location.href="NewFile.jsp";
    </script>    
    <%    
        
    }
    }
%>



</body>
</html>
package P;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
        
        public static Connection getConnection() {
            try {
                //��������
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();

            }catch(ClassNotFoundException | InstantiationException | IllegalAccessException e) {            
                e.printStackTrace();
            }
            String user="sa";
            String password="123456zz";
            String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=JavaWebDemo";
            Connection connection=null;
            try {
                //�������Ӷ���connection
                connection=DriverManager.getConnection(url, user, password);
                
            }catch(SQLException e) {
                
                e.printStackTrace();
                
            }
            return connection;
        }
        //�ر���Դ�ķ���
        public static void close(Connection connection) {
            
            if(connection!=null) {
                
                try {
                    connection.close();
                } catch (SQLException e) {
                    // TODO �Զ����ɵ� catch ��
                    e.printStackTrace();
                }
                
            }
        }
        public static void close(PreparedStatement preparedStatement) {
            
            if(preparedStatement !=null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    // TODO �Զ����ɵ� catch ��
                    e.printStackTrace();
                }
            }
            
            
        }
        public static void close(ResultSet resultSet) {
                
                if(resultSet!=null) {
                    try {
                        resultSet.close();
                    } catch (SQLException e) {
                        // TODO �Զ����ɵ� catch ��
                        e.printStackTrace();
                    }
                }
                
            }
            
        
            
    }
    
package P;

public class User {

    public String name;
    public String teacher;
    public String location;
    
    package P;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class UserUtil extends User{
static    public void add(User u) {
        
        Connection con=DBUtil.getConnection();
        String name=u.name;//�γ�
        String location=u.location ;
        String teacher=u.teacher;
        String sql="insert into Table2(name,location,teacher)values('"+name+"','"+location+"','"+teacher+"')";
        PreparedStatement preStmt = null;
        try {
            preStmt=con.prepareStatement(sql);
        } catch (SQLException e) {
            // TODO �Զ����ɵ� catch ��
            e.printStackTrace();
        }
        try {
            preStmt.executeUpdate();
        } catch (SQLException e) {
            // TODO �Զ����ɵ� catch ��
            e.printStackTrace();
        }
        
    }
static    public boolean check(User u) {
        System.out.println(u.teacher);
    boolean flag=false;
    if(u.teacher.toString().equals("������")||u.teacher.toString().equals("������")||u.teacher.toString().equals("����")||u.teacher.toString().equals("���ӹ�")||u.teacher.toString().equals("����")) {
        flag=true;    
    }
    flag=false;
    if(
            u.location.toString().startsWith("����")||u.location.toString().startsWith("����")||u.location.toString().startsWith("һ��")||u.location.toString().startsWith("����")
            ) {
        
        
        flag=true;
    }
    
    
    return flag;    
    }
    
    
}

    
    
}

I. 计划
需求描述
实现简单的增加课程功能,用户在界面内输入课程的名字,上课的地点,任课教师,然后点击提交按钮,对数据进行一下核查,如果符合要求,那么将其保存到数据库中。
估计开发时间
预计一小时之内可以完成
时间记录日志
11.28 上午9.10-9.50 进行简单的编码
11.28 上午9.50-10.10 与同学进行交流
11.28 上午10.14-10.32 补充编码
II. 开发
缺陷日志
1 数据库的存贮问题 varchar 与 nvarchar 的区别
2 method方法 get与post错误(花费了大量的时间来排查错误)
III. 总结
现在对JavaWeb项目的流程仍然不太熟悉,需要进一步联系掌握。
时间上所用略长,但是原因是由于自己的知识不清晰,不了解jsp的机制。
post与get需要进一步了解。
数据库的nvarchar与varchar的区别需要查询。

Responses