• SQL ----- JDBC 用ID查询某条记录


    package demo;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
    
    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 Edit
     */
    @WebServlet("/edit.do")
    public class Edit extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    
    	/**
    	 * @see HttpServlet#HttpServlet()
    	 */
    	public Edit() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    
    	/**
    	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
    	 *      response)
    	 */
    	protected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
    		//根据ID查询
    		String id = request.getParameter("id");//获取参数--需要修改得记录的ID
    		System.out.println(id);
    
    		//设置数据库连接参数
    		String url = "jdbc:mysql://localhost:3306/库名?serverTimezone=UTC";
    		String user = "用户名";
    		String password = "密码";
    
    		//加载数据库驱动
    		try {
    			Class.forName("com.mysql.jdbc.Driver");// 加载数据库的JDBC驱动程序
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		}
    		
    		Customer customer = new Customer();
    
    		try (Connection connection = DriverManager.getConnection(url, user, password)) {//连接数据库
    
    			//设置预处理语句,customers为表名
    			String sql = "SELECT*FROM customers where customerID= ? ";
    			PreparedStatement statement = connection.prepareStatement(sql);//预处理sql语句
    			/*
    			 * statement.setInt(1, Integer.parseInt(id));//Integer.parseInt(id)将字符串类型转换成整型
    			 */
    			int _id = Integer.parseInt(id);
    			statement.setInt(1, _id);
    			ResultSet rs = statement.executeQuery();
    
    			while (rs.next()) {
    				// 将数据库的数据转换成POJO实例
    				customer.setCustomerID(rs.getInt("CustomerID"));
    				customer.setCustomerName(rs.getString("CustomerName"));
    				customer.setContactName(rs.getString("ContactName"));
    				customer.setAddress(rs.getString("Address"));
    				customer.setCity(rs.getString("City"));
    				customer.setPostalCode(rs.getString("PostalCode"));
    				customer.setCountry(rs.getString("Country"));
    
    			} 
    			
    			rs.close();
    			statement.close();
    			
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		
    		request.setAttribute("customer", customer);//将获取POJO实例传递到edit界面
    		request.getRequestDispatcher("edit.jsp").forward(request, response);
    	}
    
    }
    
  • 相关阅读:
    ASP.NET中常用的优化性能的方法
    把WinDbg集成到Visual Studio中
    提高ASP.net的性能
    Msn Library
    [转帖]OutOfMemoryException问题的处理
    一完美的关于请求的目录不存在而需要url重写的解决方案!
    在 ASP.NET 中执行 URL 重写
    转 内存不断上升处理方法
    IIS 6 通配符应用映射和HttpHandler配置
    Java开源
  • 原文地址:https://www.cnblogs.com/max-hou/p/10849042.html
Copyright © 2020-2023  润新知