• jsp第十次作业


    package Servlet;
    
    import  java.sql.Connection;
    
    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 javax.swing.*;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    
    @WebServlet(name = "LoginServlet")
    public class RegisterServlet extends HttpServlet {
        public RegisterServlet() {
            super();
        }
    
        @Override
        public void destroy() {
            super.destroy();
        }
        public void ww() {
            String msg = "注册成功!";
            int type = JOptionPane.YES_NO_OPTION;
            JOptionPane.showMessageDialog(null, msg, title, type);
        }
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            JDBC jdbc = new JDBC();
            Connection con = null;
            PreparedStatement ps = null;
            String username=new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
            String PASSWORD=new String(request.getParameter("userpassword").getBytes("ISO-8859-1"),"UTF-8");
            String email=new String(request.getParameter("Email").getBytes("ISO-8859-1"),"UTF-8");
            try {
                con = jdbc.getConnection();
                String sql = "insert into Usersd(username,PASSWORD,email) values (?,?,?)";
                ps = con.prepareStatement(sql);
                ps.setString(1, username);
                ps.setString(2,password);
                ps.setString(3, email);
                int row = ps.executeUpdate();
                wrong1();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                jdbc.closeAll(con,ps,null);
            }
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request, response);
        }
    }
    package cn;
    
    import java.sql.*;
    
    public class JDBC {
        //获取连接
        public Connection getConnection() {
            Connection conn = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                // 建立连接
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/mysql", "root", "root");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return conn;
        }
    
        public void closeAll(Connection con, PreparedStatement ps, ResultSet rs) {
    
            //关闭资源,释放资源
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) { 
                    rs = null;
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) { 
                    ps = null;
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) { 
                    rs = null;
                }
            }
        }
    }

  • 相关阅读:
    算法笔记_148:有向图欧拉回路求解(Java)
    算法笔记_147:有向图欧拉回路判断应用(Java)
    算法笔记_146:TarJan算法的应用(Java)
    算法笔记_145:拓扑排序的应用(Java)
    第十六章、例行性工作排程 (crontab)
    第十五章、磁碟配额(Quota)与进阶文件系统管理
    第十四章、Linux 账号管理与 ACL 权限配置
    第十三章、学习 Shell Scripts
    第十二章、正规表示法与文件格式化处理
    第十一章、认识与学习 BASH
  • 原文地址:https://www.cnblogs.com/xffzcjlove/p/13169026.html
Copyright © 2020-2023  润新知