时间:2021-07-01 10:21:17 帮助过:2人阅读
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页面获取数据插入数据库的小程序
标签:cat app 简单 ack password arch dex const http