一个简单的从web页面获取数据插入数据库的小程序

Posted hl若水

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个简单的从web页面获取数据插入数据库的小程序相关的知识,希望对你有一定的参考价值。

1.数据库新建一张表:Student_information,表中三个字段,name(nvarchar(50),null),sex(nvarchar(50),null),age(numeric(18,0),null)

2.先新建一个Main.jsp.

<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>

</head>
<body>
<form action ="EmployeServlet" method="post">
  <h1 align ="center" id ="biaotou">增加学生信息</h1>
  <table align="center">
    <tr>
        <td>姓名:<td>
        <td><input type="text" name="emplyname" value=""/></td>
    <tr> 
    <tr>
        <td>性别:</td>
        <td><input type="radio" name="sex" value="男"><input type="radio" name="sex" value="女"></td>
    </tr>
    <tr>
        <td>年龄:<td>
        <td><input type="text" name="age" value=""/></td>
    <tr> 
    <tr>
        <td><td>
        <td><button  type="submit"  value="submit">提交</button></td>
    <tr> 
  </table>
</form>

</body>

</html>

3.新建一个Employ类,存放学生属性

package testjsp;

public class Employ {
	private String name;
	private  String sex;
	private  int age; 
 
    public void  setName(String name){
    	 this.name=name;
    }
    public String getName(){
    	return this.name;
    }
    public void  setSex(String sex){
    	 this.sex=sex;
    }
    public String getSex(){
    	return this.sex;
    }
    public void setAge(int age){
    	this.age=age;
    }
    public int getAge(){
    	return this.age;
    }
    
}

4.新建一个TestJsp类,获取数据,连接数据库,插入数据

package testjsp;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import test.EmployeServlet;

public class TestJsp {
  
   
  
   public boolean addEmploy(Employ ee){
       System.out.println(ee.getName());
       System.out.println(ee.getSex());
       System.out.println(ee.getAge());
       String a=ee.getName();
       String b=ee.getSex();
       int c=ee.getAge();
       System.out.println(a+b+c);
       String URL="jdbc:sqlserver://localhost:1433;DatabaseName=TEST_CAL";
       String USER="sa";
       String PASSWORD="XXXXXX";
       Connection conn=null;
       PreparedStatement pstmt=null;
       try{
           //加载驱动程序
           Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
           //获得数据库连接
           conn=(Connection)DriverManager.getConnection(URL,USER,PASSWORD);
           System.out.println(conn);
           String sql="insert into Student_information (name,sex,age) values(?,?,?)";
           pstmt=conn.prepareStatement(sql);
           pstmt.setString(1,ee.getName());
           pstmt.setString(2,ee.getSex());
           pstmt.setInt(3, ee.getAge());
           pstmt.execute();
       }catch(ClassNotFoundException e){
           e.printStackTrace();
       }catch(SQLException e){
           e.printStackTrace();
       }
       
      
      
       return true;
   }
   public static void main(String[] args) {
     //  TestJsp n=new TestJsp();
     //  n.addEmploy();
}
}

5.新建一个Servlet类

package test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import testjsp.Employ;
import testjsp.TestJsp;

/**
 * Servlet implementation class EmployeServlet
 */
@WebServlet("/EmployeServlet")
public class EmployeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EmployeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
    //      String name=new String(request.getParameter("emplyname").getBytes("iso-8859-1"),"utf-8");//搜到的答案,不知道怎么用
         String name=request.getParameter("emplyname");
         String sex=request.getParameter("sex");
         String age=request.getParameter("age");         
         Employ em=new Employ();
         em.setName(name);
         em.setSex(sex);
         em.setAge(Integer.parseInt(age));         
         System.out.println(name);
         System.out.println(sex);
         System.out.println(Integer.parseInt(age));         
         TestJsp te=new TestJsp();
         te.addEmploy(em);        
    }  
}

6.新建一个web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app>

<welcome-file-list>
<welcome-file>MainPage.jsp</welcome-file>
</welcome-file-list>
<servlet>
<!-- servlet名称,与servlet-mapping中的servlet-name必须一致 -->
<servlet-name>EmployeServlet</servlet-name>
<!--Servlet类的位置-->
<servlet-class>test.EmployeServlet</servlet-class>
</servlet>
<servlet-mapping>
<!-- servlet名称,与上面中的servlet-name必须一致 -->
<servlet-name>EmployeServlet</servlet-name>

<url-pattern>/EmployeServlet/</url-pattern>
</servlet-mapping>
</web-app>

 
 

 

以上是关于一个简单的从web页面获取数据插入数据库的小程序的主要内容,如果未能解决你的问题,请参考以下文章

我的第一个python web开发框架——一个简单的小外包

模拟web请求——简单的小程序提高工作效率

爬虫:工作中编写的一个python爬取web页面信息的小demo

web前端面试,被问到ajax如果获取到一个页面的内容,如何渲染到页面呢

java web 如何防止 用户绕过js验证,直接地址栏提交表单或自己编写html页面,提交数据到服务器?

我想使用我的角度4应用程序作为其他网站的小部件 - 无论网站语言 -