package demo;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
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 Queryalldata
*/
@WebServlet("/queryalldata.jsp")
public class Queryalldata extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public Queryalldata() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//查询所有数据 //设置数据库连接参数
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();
} List<Customer> list = new ArrayList<Customer>();//初始化一个Customer线性列表 try(Connection connection=DriverManager.getConnection(url, user, password)){//连接数据库
Statement statement = connection.createStatement();
String sql="SELECT*FROM customers";//查询所有数据的sql语句,customers为表名
ResultSet rs=statement.executeQuery(sql); //遍历数据库所有数据,并添加到列表list中
while(rs.next()) {
Customer customer = new Customer();
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"));
list.add(customer);
} rs.close();
statement.close(); }catch(SQLException e) {
e.printStackTrace();
} String msg =(String) request.getAttribute("msg");
if(msg!=null || msg !=" ")
request.setAttribute("msg", msg); //将有数据的列表传递到显示记录所有界面
request.setAttribute("customers", list);
request.getRequestDispatcher("all.jsp").forward(request, response);
} }

SQL -------- JDBC 查询所有记录的更多相关文章

  1. Java开发笔记(一百四十八)通过JDBC查询数据记录

    前面介绍了通过JDBC如何管理数据库,当时提到Statement专门提供了executeQuery方法用于查询操作,为什么查询操作这么特殊呢?这是因为其它语句跑完一次就了事了,顶多像insert.up ...

  2. 如何使用JDBC查询所有记录

    public class JdbcDao {    private Connection conn=null;   //数据库连接对象    private String strSql=null; / ...

  3. SQL语句 查询最新记录

    要求:SQL语句按ID以最新时间查询最新的一条记录 方法1: select * from (select *, ROW_NUMBER() over(partition by id order by u ...

  4. Sql Server 查询重复记录

    参考网址:http://database.51cto.com/art/201103/250046.htm SQL Server数据库多种方式查找重复记录 select * from dbo.T0058 ...

  5. sql 根据查询的记录生成序号的几种方式

    row_number()  order() 函数会为查询出来的每一行记录生成一个序号,依次排序且不会重复,注意使用row_number函数时必须要用over子句选择对某一列进行排序才能生成序号. ra ...

  6. SQL简单查询后续记录

    --首先创建数据库TEST CREATE DATABASE TEST --创建表tb_user USE TEST CREATE TABLE [tb_user]( [name] [nvarchar] ( ...

  7. 关于sql的查询操作记录

    1.--读取库中的所有表名 select name from sysobjects where xtype='u'  --读取指定表的所有列名 select name from syscolumns  ...

  8. 使用sql语句查询日期在一定时间内的数据

    使用sql语句查询日期在一周内的数据 select * from ShopOrder where datediff(week,ordTime,getdate()-1)=0   //查询当天日期在一周年 ...

  9. SQL提高查询效益之in、not in、between、like等条件讲述

    在使用SQL语句查询数据库记录时,如果要查询相同的内容,有着不同的多种方法. 仍然,尽管使用多种方法可以得到相同的结果,但是,如果您使用不同的方法,在执行效益上是截然不同的.因此,我们得仔细考虑,如果 ...

随机推荐

  1. 长春理工大学第十四届程序设计竞赛H Arithmetic Sequence——使用特例

    题目 链接 题意:给定一个数X,输出一个等差数列,使得和为X. 分析 由等差数列的定义,可见一个数就是等差数列,两个数也是等差数列 #include<bits/stdc++.h> usin ...

  2. 2018多校第十场 HDU 6430 (线段树合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6430 题意:一棵树上每个节点权值为v[i],每个节点的heard值是:以它为LCA的两个节点的GCD的 ...

  3. QT 设置应用程序图标和可执行程序图标

    1, 首先准备个ICO图标.例如:myappico.ico 在工程目录下新建images文件夹并你的图标myappico.ico(只能用ico格式的图片)放到工程目录下的images文件夹下 2, 用 ...

  4. Codeforces Round #589 (Div. 2) E. Another Filling the Grid(DP, 组合数学)

    链接: https://codeforces.com/contest/1228/problem/E 题意: You have n×n square grid and an integer k. Put ...

  5. SCOI2014 bzoj3594 方伯伯的玉米田(二维树状数组+dp)

    3594: [Scoi2014]方伯伯的玉米田 Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 1971  Solved: 961[Submit][St ...

  6. Python基础面试题整理

    基础 Python中lambda是什么意思 Python中的pass是什么意思 作为解释型语言,Python如何运行 什么是Python的单元测试 在Python中unittest是什么 如何将数字转 ...

  7. 预处理、const、static、sizeof-说明内联函数使用的场合

    1:首先使用inline函数可以完全取代表达式形式的宏定义. 内联函数在C++类中的应用最广的应该是用来定义存取函数.我们定义的类中一般会把数据成员定义成私有的或者保护的,这样,外界就不能直接读写我们 ...

  8. nginx 实现高并发和高负载

    一.Nginx是如何实现高并发的 service nginx start之后,然后输入#ps -ef|grep nginx,会发现Nginx有一个master进程和若干个worker进程,这些work ...

  9. Nginx之HTTP过滤模块

    1. HTTP 过滤模块 ngx_http_not_modified_module 仅对 HTTP 头部做处理.在返回 200 成功时,根据请求中 If-Modified-Since 或者 If-Un ...

  10. qt 元对象系统

    元对象系统 Qt中的元对象系统是用来处理对象间通讯的信号/槽机制.运行时的类型信息和 动态属性系统. 它基于下列三类: QObject类: 类声明中的私有段中的Q_OBJECT宏: 元对象编译器(mo ...