zoukankan      html  css  js  c++  java
  • SQL -------- JDBC 修改某条记录得内容

    package demo;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class UpdateCustomer
     */
    @WebServlet("/updateCustomer.do")
    public class UpdateCustomer extends HttpServlet {
    	private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public UpdateCustomer() {
            super();
            // TODO Auto-generated constructor stub
        }
    
    	/**
    	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		
    		//获取修改后的参数
    		String CustomerID =request.getParameter("CustomerID");
    		String CustomerName = request.getParameter("CustomerName");
    		String ContactName= request.getParameter("ContactName");
    		String Address = request.getParameter("Address");
    		String City = request.getParameter("City");
    		String PostalCode = request.getParameter("PostalCode");
    		String Country=request.getParameter("Country");
    		
    		//设置数据库连接参数
    		String url="jdbc:mysql://localhost:3306/库名?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
    		String user = "用户名";
    		String password="密码";
    		
    		//加载数据库驱动
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		}
    		try(Connection connection =DriverManager.getConnection(url, user, password)){//连接数据库
    			//where指定条件
    			//修改sql语句,customers为表名
    			 String sql = "update Customers set CustomerName=?, ContactName=?, Address=?,City=?, PostalCode=?,Country=? where customerID=?;";
    	            PreparedStatement statement = connection.prepareStatement(sql);//预处理sql语句
    					//以下语句对应VALUES语句里的值
    			        statement.setString(1, CustomerName);
    			        statement.setString(2, ContactName);
    			        statement.setString(3, Address);
    			        statement.setString(4, City);
    			        statement.setString(5, PostalCode);
    			        statement.setString(6, Country);
    			        statement.setInt(7, Integer.parseInt(CustomerID));
    			        int value = statement.executeUpdate();//executeUpdate执行修改,将修改的个数传给value
    			        
    			        statement.close();// 关闭statement,释资源
    			        
    		}catch(SQLException e) {
    			e.printStackTrace();
    		}
    		request.getRequestDispatcher("queryalldata.jsp").forward(request, response);//跳转到查询所有得服务程序
    	}
    
    }
    
  • 相关阅读:
    解决FileReader读取文件乱码问题
    comparable 与 comparator
    JavaScript添加水印
    mybatis里oracle与MySQL的insert_update
    关于回车换行
    solr创建collection
    hbase相关配置说明
    java基础(十一)--- IO
    java基础(十)--- 异常
    c++中的vector原理
  • 原文地址:https://www.cnblogs.com/max-hou/p/10849051.html
Copyright © 2011-2022 走看看