-
Notifications
You must be signed in to change notification settings - Fork 72
SELECT命令与JDBC使用 #190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zdmhahaha
wants to merge
2
commits into
master
Choose a base branch
from
zdmgo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
SELECT命令与JDBC使用 #190
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
|
|
||
| package com.github.hcsp.sql; | ||
|
|
||
| import java.io.File; | ||
| import java.math.BigDecimal; | ||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.SQLException; | ||
| import java.sql.*; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Sql { | ||
|
|
@@ -82,14 +82,20 @@ public String toString() { | |
| // | 2 | | ||
| // +-----+ | ||
| public static int countUsersWhoHaveBoughtGoods(Connection databaseConnection, Integer goodsId) throws SQLException { | ||
| return 0; | ||
| PreparedStatement statement = databaseConnection.prepareStatement("select count(distinct user_id) from `order` where goods_id = ?"); | ||
| statement.setInt(1, goodsId); | ||
| ResultSet resultSet = statement.executeQuery(); | ||
| while (resultSet.next()) { | ||
| return resultSet.getInt(1); | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| /** | ||
| * 题目2: | ||
| * 分页查询所有用户,按照ID倒序排列 | ||
| * | ||
| * @param pageNum 第几页,从1开始 | ||
| * @param pageNum 第几页,从1开始 | ||
| * @param pageSize 每页有多少个元素 | ||
| * @return 指定页中的用户 | ||
| */ | ||
|
|
@@ -100,7 +106,21 @@ public static int countUsersWhoHaveBoughtGoods(Connection databaseConnection, In | |
| // | 1 | zhangsan | tel1 | beijing | | ||
| // +----+----------+------+----------+ | ||
| public static List<User> getUsersByPageOrderedByIdDesc(Connection databaseConnection, int pageNum, int pageSize) throws SQLException { | ||
| return null; | ||
| PreparedStatement statement = databaseConnection.prepareStatement("select id, name, tel ,address from `user` ORDER BY ID DESC limit ?,?"); | ||
| statement.setInt(1, pageSize * (pageNum - 1));//2 2,2 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| statement.setInt(2, pageSize);//2 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| ResultSet resultSet = statement.executeQuery(); | ||
|
|
||
| List<User> list = new ArrayList<>(); | ||
| while (resultSet.next()) { | ||
| User user = new User(); | ||
| user.id = resultSet.getInt(1); | ||
| user.name = resultSet.getString(2); | ||
| user.tel = resultSet.getString(3); | ||
| user.address = resultSet.getString(4); | ||
| list.add(user); | ||
| } | ||
| return list; | ||
| } | ||
|
|
||
| // 商品及其营收 | ||
|
|
@@ -132,7 +152,17 @@ public String toString() { | |
| // | 3 | goods3 | 20 | | ||
| // +----+--------+------+ | ||
| public static List<GoodsAndGmv> getGoodsAndGmv(Connection databaseConnection) throws SQLException { | ||
| return null; | ||
| PreparedStatement statement = databaseConnection.prepareStatement("select goods_id ,name,sum(goods_num * goods_price) as GMV from `order` join goods on `order`.goods_id = goods.id group by goods_id order by GMV desc"); | ||
| ResultSet resultSet = statement.executeQuery(); | ||
| List<GoodsAndGmv> list = new ArrayList<>(); | ||
| while (resultSet.next()) { | ||
| GoodsAndGmv goodsAndGmv = new GoodsAndGmv(); | ||
| goodsAndGmv.goodsId = resultSet.getInt(1); | ||
| goodsAndGmv.goodsName = resultSet.getString(2); | ||
| goodsAndGmv.gmv = resultSet.getBigDecimal(3); | ||
| list.add(goodsAndGmv); | ||
| } | ||
| return list; | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -170,7 +200,18 @@ public String toString() { | |
| // | 6 | zhangsan | goods3 | 20 | | ||
| // +----------+-----------+------------+-------------+ | ||
| public static List<Order> getInnerJoinOrders(Connection databaseConnection) throws SQLException { | ||
| return null; | ||
| PreparedStatement statement = databaseConnection.prepareStatement("select `order`.id, user.name, goods.name,goods_price*goods_num as total from `order` join user on `order`.user_id = user.id join goods on `order`.goods_id = goods.id"); | ||
| ResultSet resultSet = statement.executeQuery(); | ||
| List<Order> list = new ArrayList<>(); | ||
| while (resultSet.next()) { | ||
| Order order = new Order(); | ||
| order.id = resultSet.getInt(1); | ||
| order.userName = resultSet.getString(2); | ||
| order.goodsName = resultSet.getString(3); | ||
| order.totalPrice = resultSet.getBigDecimal(4); | ||
| list.add(order); | ||
| } | ||
| return list; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -198,7 +239,18 @@ public static List<Order> getInnerJoinOrders(Connection databaseConnection) thro | |
| // | 8 | NULL | NULL | 60 | | ||
| // +----------+-----------+------------+-------------+ | ||
| public static List<Order> getLeftJoinOrders(Connection databaseConnection) throws SQLException { | ||
| return null; | ||
| PreparedStatement statement = databaseConnection.prepareStatement("select `order`.id, user.name, goods.name,goods_price*goods_num as total from `order` left join user on `order`.user_id = user.id left join goods on `order`.goods_id = goods.id"); | ||
| ResultSet resultSet = statement.executeQuery(); | ||
| List<Order> list = new ArrayList<>(); | ||
| while (resultSet.next()) { | ||
| Order order = new Order(); | ||
| order.id = resultSet.getInt(1); | ||
| order.userName = resultSet.getString(2); | ||
| order.goodsName = resultSet.getString(3); | ||
| order.totalPrice = resultSet.getBigDecimal(4); | ||
| list.add(order); | ||
| } | ||
| return list; | ||
| } | ||
|
|
||
| // 注意,运行这个方法之前,请先运行mvn initialize把测试数据灌入数据库 | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不应使用 '.*' 形式的导入 - java.sql.* 。