• 在servlet连接mysql下的最简单增删查改


    添加

    package com.jquery.register;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class add extends HttpServlet {
    
        public void destroy() {
            super.destroy(); 
        }
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
                doPost(request,response);
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            request.setCharacterEncoding("utf-8"); 
             response.setCharacterEncoding("utf-8");
             Connection conn = null;
             PreparedStatement stmt = null;
             ResultSet rs = null;
             String name="li";
             String password="12345";
             String drive = "com.mysql.jdbc.Driver";
             System.out.println("ajax后台交互成功");  
             try{
                 Class.forName(drive);
                 conn = DriverManager
                         .getConnection("jdbc:mysql://localhost:3306/test?user=root&password=123");
                 System.out.println("数据库连接成功");
                 String sql="insert  into user_table(name,password)";
                 sql+=" values(?,?)";
                 stmt = conn.prepareStatement(sql);
                 stmt.setString(1,name);
                 stmt.setString(2,password);
                 stmt.executeUpdate();
                 System.out.println("success");
    //        out.flush();
    //        out.close();
        }catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    //注意关闭原则:从里到外
                        if (rs != null) {
                            rs.close();
                        }
                        if (stmt != null) {
                            stmt.close();
                        }
                        if (conn != null) {
                            conn.close();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
        }
        public void init() throws ServletException {
            // Put your code here
        }
    
    }

    删除

    package com.jquery.register;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class delete extends HttpServlet {
    
        public void destroy() {
            super.destroy();
        }
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
                doPost(request,response);
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            request.setCharacterEncoding("utf-8");
             response.setCharacterEncoding("utf-8");
             Connection conn = null;
             PreparedStatement stmt = null;
             ResultSet rs = null;
             String drive = "com.mysql.jdbc.Driver";
             System.out.println("ajax后台交互成功");  
             try{
                 Class.forName(drive);
                 conn = DriverManager
                         .getConnection("jdbc:mysql://localhost:3306/test?user=root&password=123");
                 System.out.println("数据库连接成功");
                 String sql="delete  from user_table where name='zhang'";
                 stmt = conn.prepareStatement(sql);
                 stmt.executeUpdate();
                 System.out.println("success");
    //        out.flush();
    //        out.close();
        }catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    //注意关闭原则:从里到外
                        if (rs != null) {
                            rs.close();
                        }
                        if (stmt != null) {
                            stmt.close();
                        }
                        if (conn != null) {
                            conn.close();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
        }
        public void init() throws ServletException {
            // Put your code here
        }
    
    }

    查找

    package com.jquery.register;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class search extends HttpServlet {
    
        public void destroy() {
            super.destroy(); 
        }
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
                doPost(request,response);
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            request.setCharacterEncoding("utf-8"); 
             response.setCharacterEncoding("utf-8");
             Connection conn = null;
             PreparedStatement stmt = null;
             ResultSet rs = null;
             
             String drive = "com.mysql.jdbc.Driver";
             System.out.println("ajax后台交互成功");  
             try{
                 Class.forName(drive);
                 conn = DriverManager
                         .getConnection("jdbc:mysql://localhost:3306/test?user=root&password=123");
                 System.out.println("数据库连接成功");
                 String sql="select  *  from user_table ";
                 
                 stmt = conn.prepareStatement(sql);
                 
                 rs=stmt.executeQuery();
                 
                 System.out.println("success");
                 while(rs.next()){
                     String name = rs.getString(1);
                    String password = rs.getString(2);
                    System.out.println(name + ", " + password);
                 }
    //        out.flush();
    //        out.close();
        }catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    //注意关闭原则:从里到外
                        if (rs != null) {
                            rs.close();
                        }
                        if (stmt != null) {
                            stmt.close();
                        }
                        if (conn != null) {
                            conn.close();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
        }
        public void init() throws ServletException {
            // Put your code here
        }
    
    }

    修改

    package com.jquery.register;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class update extends HttpServlet {
    
        public void destroy() {
            super.destroy(); 
        }
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
                doPost(request,response);
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            request.setCharacterEncoding("utf-8"); 
             response.setCharacterEncoding("utf-8");
             Connection conn = null;
             PreparedStatement stmt = null;
             ResultSet rs = null;
             String password="654321";
             String drive = "com.mysql.jdbc.Driver";
             System.out.println("ajax后台交互成功");  
             try{
                 Class.forName(drive);
                 conn = DriverManager
                         .getConnection("jdbc:mysql://localhost:3306/test?user=root&password=123");
                 System.out.println("数据库连接成功");
                 String sql="update user_table set password='"+password+"' where name='li'";
                 
                 stmt = conn.prepareStatement(sql);
                 
                 stmt.executeUpdate();
                 
                 System.out.println("success");
                 
    //        out.flush();
    //        out.close();
        }catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    //注意关闭原则:从里到外
                        if (rs != null) {
                            rs.close();
                        }
                        if (stmt != null) {
                            stmt.close();
                        }
                        if (conn != null) {
                            conn.close();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
        }
        public void init() throws ServletException {
            // Put your code here
        }
    
    }

  • 相关阅读:
    google protobuf
    spawn-fcgi和libfcgi源码解读
    [Linux] 查看进程的上下文切换pidstat
    [MySQL] update语句的redo log过程
    [转载] PHP 8新特性之JIT简介
    [PHP] 新浪企邮webmail在memcache实践使用共享session
    [Go] Golang练习项目-web客服系统即时通讯websocket项目go-fly
    [PHP] php8的jit不支持32位系统WARNING: JIT not supported by host architecture
    [PHP] 源码编译安装opcache
    [PHP] 查找使用的哪个配置文件php.ini
  • 原文地址:https://www.cnblogs.com/liurg/p/7910051.html
Copyright © 2020-2023  润新知