• 一个简单的从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>

     
     
  • 相关阅读:
    BeautifulSoup的基本用法
    [leedcode 189] Rotate Array
    [leedcode 187] Repeated DNA Sequences
    [leedcode 179] Largest Number
    [leedcode 174] Dungeon Game
    [leedcode 173] Binary Search Tree Iterator
    [leedcode 172] Factorial Trailing Zeroes
    [leedcode 171] Excel Sheet Column Number
    [leedcode 169] Majority Element
    [leedcode 168] Excel Sheet Column Title
  • 原文地址:https://www.cnblogs.com/Hebe-Huang/p/7930259.html
Copyright © 2020-2023  润新知