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.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("/found.do")
    public class found extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    
    	/**
    	 * @see HttpServlet#HttpServlet()
    	 */
    	public found() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    
    	/**
    	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
    	 *      response)
    	 */
    	protected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
    		//获取用来查询记录的参数
    		String name = request.getParameter("name");
    		System.out.println(name);
    
    		//设置数据库连接参数
    		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");// 加载数据库的JDBC驱动程序
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		}
    		
    		Customer customer = new Customer();
    
    		try (Connection connection = DriverManager.getConnection(url, user, password)) {//连接数据库
    
    			//设置sql语句,查询CustomerName为name的记录的数据,customers为表名
    			String sql = "SELECT*FROM customers where CustomerName= ? ";
    			PreparedStatement statement = connection.prepareStatement(sql);
    			
    			statement.setString(1, name);
    			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);//将获取到的参数封装成对象传递到查找到得记录的界面
    		request.getRequestDispatcher("showone.jsp").forward(request, response);
    	}
    
    }
    
  • 相关阅读:
    今发现“最全前端资源汇集”,果断收藏
    js基础
    重排版与重绘
    小乌龟的配置
    考试网站
    苹果手机上时间的兼容
    自定义alert
    [概率dp] 流浪地球
    [权值线段树] 1163B2 Cat Party (Hard Edition)
    [单调栈]1156E Special Segments of Permutation
  • 原文地址:https://www.cnblogs.com/max-hou/p/10849064.html
Copyright © 2011-2022 走看看