简单的利用jsp连接数据库(jdbc)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chaiwuxuan/article/details/52097288

第一步s:在数据苦衷创建表(名子为:uses)

第二步创建表对应的实体类:

package com.cn.vo;

public class Usersvo {
 private int id;
 private String name;
 private int age;
 private String adress;
 private String tel;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public String getAdress() {
  return adress;
 }
 public void setAdress(String adress) {
  this.adress = adress;
 }
 public String getTel() {
  return tel;
 }
 public void setTel(String tel) {
  this.tel = tel;
 }
 
}
第三步:创建添加数据的jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>添加用户信息</title>
  </head>
 
  <body >
  <h1>请添加用户信息:</h1>
    <form action = "AddUsersServlet" method = "post">
    <table border = "1" align = "center">
    <tr>
    <td>编号:<input type = "text" name = "id"></td>
    </tr>
    <tr>
    <td>姓名:<input type = "text" name = "name"></td>
    </tr>
    <tr>
    <td>年龄:<input type = "text" name = "age"></td>
    </tr>
    <tr>
    <td>电话:<input type = "text" name = "tel"></td>
    </tr>
    <tr>
    <td>地址:<input type = "text" name = "adress"></td>
    </tr>
    <tr>
    <td>
    <input type = "submit" value = "提交">
    <input type = "reset" value = "重置">
    </td>
    </tr>
    </table>
    </form>
  </body>
</html>
第四步:编写添加数据的Servlet

package com.cn.add;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

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

import com.cn.vo.Usersvo;

public class AddUsersServlet extends HttpServlet {

 
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

 
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  request.setCharacterEncoding("gb2312");
  response.setCharacterEncoding("gb2312");
  PrintWriter out = response.getWriter();
  int id = Integer.parseInt(request.getParameter("id"));
  String name = request.getParameter("name");
  int age = Integer.parseInt(request.getParameter("age"));
  String tel = request.getParameter("tel");
  String adress = request.getParameter("adress");
  
  Usersvo usersvo = new Usersvo();
  usersvo.setId(id);
  usersvo.setName(name);
  usersvo.setAge(age);
  usersvo.setTel(tel);
  usersvo.setAdress(adress);
  AddUser adduser = new AddUser();
 
   try {
    adduser.add(usersvo);
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  
  request.getRequestDispatcher("Users.jsp").forward(request, response);
  out.flush();
  out.close();
 }

 
 public void init() throws ServletException {
  // Put your code here
 }

}
第五步:编写添加数据的实现类(包含数据库连接的步骤)

package com.cn.add;

import java.sql.DriverManager;
import java.sql.SQLException;

import com.cn.vo.Usersvo;

public class AddUser {
 java.sql.Connection con =null;
 java.sql.PreparedStatement psmt = null;

 public void add(Usersvo usersvo) throws  SQLException{
 
 try{
 String Driver = "com.mysql.jdbc.Driver";
 String url = "jdbc:mysql://localhost:3306/homework?useUnicode=true&characterEncoding=utf8";
 String userName = "root";
 String pswd = "123456";
 Class.forName(Driver);
 con =  DriverManager.getConnection(url,userName,pswd);

 String sql = "insert into  users(id,name,age,tel,adress) values(?,?,?,?,?) ";
 psmt =  con.prepareStatement(sql);
 psmt.setInt(1,usersvo.getId());
 psmt.setString(2, usersvo.getName());
 psmt.setInt(3,usersvo.getAge());
 psmt.setString(4,usersvo.getTel());
 psmt.setString(5, usersvo.getAdress());
 psmt.executeUpdate();
 System.out.println("输入成功!");
 System.out.println("id:"+usersvo.getId()+" "+"name:"+usersvo.getName()+" "+"age:"+usersvo.getAge()+" "+"tel:"+usersvo.getTel()+" "+"adress:"+usersvo.getAdress());
 
 }catch(ClassNotFoundException e){
  e.printStackTrace();
  }finally{
   psmt.close();
   con.close();
  }
 }

 
}

第六步:编写处理中文乱码的过滤器:

package com.cn.filter;

import java.io.IOException;
import java.nio.file.DirectoryStream.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public abstract class Zh_filter implements Filter{
 public void destory(){
  
 }
 public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws
 IOException, ServletException{
  request.setCharacterEncoding("gb2312");
  response.setCharacterEncoding("gb2312");
  chain.doFilter(request,response);
  
 }
 public  void init(FilterConfig agr0)throws ServletException{
  
 }
 
}

这些是包和类的封装:
运行结果
在数据库中显示:

有很多不足的地方,希望有大能指示!
展开阅读全文

没有更多推荐了,返回首页