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);
} }

最新文章

  1. page文件
  2. 陕西中际现代:基于自适应算法的PLC滴灌控制系统
  3. js下拉菜单默认值为当前年份,下拉菜单的选项为当前年份往前推5年
  4. Xcode HeaderDoc 过程(1)
  5. 微信小程序手机预览请求不到数据(最后一条不明所以)
  6. Python3 tkinter基础 OptionMenu 点击按钮,出现单选的下拉列表
  7. Linux 搭建Hadoop集群 ----workcount案例
  8. php 三元运算符实例详细介绍
  9. ASP.NET CORE 之 在IIS上部署MVC项目
  10. 元组,列表的增删改查, for,range 循环
  11. 【转】oracle定制定时执行任务
  12. 《FPGA全程进阶---实战演练》第十四章 蜂鸣器操作
  13. ci 3.0 默认路由放在子文件夹 无法访问的解决办法
  14. CLH队列锁
  15. 基于jquery的json转table插件jsontotable
  16. HTML 5之meta标签viewport应用
  17. 10个实用的UX设计作品推销小窍门
  18. Firefox mobile (android) and orientationchange
  19. Ruby入门(1)——windows下Ruby开发环境搭建
  20. oracle如何给指定用户修改密码?

热门文章

  1. 使用Jieba提取文章的关键词
  2. 建造者模式(Builder)---创建型
  3. java new一个对象的过程中发生了什么
  4. 发布web项目时,关于未能加载文件或程序集或它的某一个依赖项。拒绝访问的问题
  5. js off动画事件
  6. response 下载文件火狐浏览器文件名乱码问题
  7. H5性能优化报告以及方案模板
  8. gitolite 代码访问控制
  9. Ubuntu 安装 JDK1.8
  10. kotlin array