对Javaweb的相关练习之利用.jsp文件和.java文件将输入的数据存储到指定的数据库中

练习分析

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.awt.datatransfer.DataFlavor;
import java.io.*;
import java.security.Principal;
import java.sql.*;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;

@WebServlet("/DBU")
public class DBUtil extends HttpServlet{


    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ad","root","root");

        String sql="insert into students values(?,?,?,?)";

        PreparedStatement pst = conn.prepareStatement(sql);

//        String name=request.getParameter("name");
//
//        String id=request.getParameter("id");
//
//        String age1= request.getParameter("age");
//
//        String score1= request.getParameter("score");
//
//        int age=Integer.parseInt("age1");
//
//        int score=Integer.parseInt("score1");

        pst.setString(1,"lisi");

        pst.setString(2,"2023");

        pst.setInt(3,21);

        pst.setInt(4,540);

        int i = pst.executeUpdate();

        if(i>0){
            System.out.println("添加成功!");
            pst.close();
            conn.close();
        }
        else{
            System.out.println("添加失败!");
        }
    }
}

上述代码为最基本的内容,指定某一组数,将其输送到指定的数据库中,目前还需实现,将从.jsp页面输入的内容,存储到指定的数据库的功能;

Information.java

package com.tao.enty;

public class Information {
    public String name;
    public String id;
    public Integer age;
    public Integer score;

    public void setName(String name){

        this.name=name;
    }
    public String getName(){
        return name;
    }
    public void setId(String id){
        this.id=id;
    }
    public String getId(){
        return id;
    }
    public void setAge(Integer age){
        this.age=age;
    }
    public Integer getAge(){
        return age;
    }
    public void setScore(Integer score){
        this.score=score;
    }
    public Integer getScore(){
        return score;
    }
    public Information(String name,String id,Integer age,Integer score){
        super();
        this.name=name;
        this.id=id;
        this.age=age;
        this.score=score;
    }
    public Information(){
        super();
    }
}

UserDao.java

package com.tao.dao;

import com.tao.enty.Information;

public class UserDao extends BaseDao {
    public Information dologin(String name, String id, Integer age, Integer score){
        Information in=null;
        try {
            super.connect();
            String sql="select * from students where name=? and where id=? and where age=? and where score=?";
            pst=conn.prepareStatement(sql);
            pst.setString(1, name);
            pst.setString(2,id);
            pst.setInt(3,age);
            pst.setInt(4,score);

            rs=pst.executeQuery();
            while(rs.next()){
               in=new Information();
               in.setName(rs.getString("name"));
                in.setId(rs.getString("id"));
                in.setAge(rs.getInt("age"));
                in.setScore(rs.getInt("score"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            super.closeAll();
        }
        return in;
    }
    public int insert(Information in){
        int row=0;
        try {
            super.connect();
            String sql="insert into students values(?,?,?,?)";
            pst=conn.prepareStatement(sql);
            pst.setString(1, in.getName());
            pst.setString(2,in.getId());
            pst.setInt(3, in.getAge());
            pst.setInt(4, in.getScore());
            row=pst.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            super.closeAll();
        }
        return row;
    }
}

BaseDao.java

package com.tao.dao;

import java.sql.*;

public class BaseDao {
    public Connection conn=null;
    public Statement stmt=null;
    public ResultSet rs=null;

    public String url="jdbc:mysql://localhost:3306/ad";
    public String username="root";
    public String password="20214063";
    public PreparedStatement pst=null;

    public void connect(){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, username, password);
            stmt = conn.createStatement();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void closeAll(){
        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
            if(pst!=null){
                pst.close();
            }
        }catch(Exception e) {

        }
    }

}

index.jsp

<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: 12140
  Date: 2022/9/22
  Time: 10:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>注册页面</title>
  </head>
  <body>

    <h1>添加学生信息</h1>
    <form action="FenXi.jsp" method="post">
      姓名:<input type="text" name="name"/><br/>

      学号:<input type="text" name="id"/><br/>

      年龄:<input type="text" name="age"/><br/>

      成绩:<input type="text" name="score"/><br/>

      <input type="submit" name="smt" value="提交"/>

    </form>


  </body>
</html>


FenXi.jsp

<%@ page import="com.tao.enty.Information"%>
<%@ page import="com.tao.dao.UserDao"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%
    request.setCharacterEncoding("UTF-8");
    Integer age=Integer.valueOf(request.getParameter("age"));
    Integer score=Integer.valueOf(request.getParameter("score"));
String name=request.getParameter("name");
String id=request.getParameter("id");


Information i =new Information();
    i.setAge(age);
    i.setScore(score);
i.setName(name);
i.setId(id);

UserDao ud=new UserDao();

int x=ud.insert(i);

if(x!=0){
    request.getRequestDispatcher("/success.jsp").forward(request, response);
}
else{
    request.getRequestDispatcher("/Fail.jsp").forward(request, response);
}
%>

</body>
</html>

本次的代码可以实现将在jsp页面中输入的内容,存储到指定数据库中去的功能!

posted @ 2022-09-23 10:03  yesyes1  阅读(24)  评论(0编辑  收藏  举报