Skip to content
Open

'sql' #162

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 116 additions & 8 deletions src/main/java/com/github/hcsp/sql/Sql.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@

package com.github.hcsp.sql;
import com.sun.java.browser.plugin2.liveconnect.v1.Result;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无用导入 - com.sun.java.browser.plugin2.liveconnect.v1.Result 。

import com.sun.org.apache.xpath.internal.operations.Or;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无用导入 - com.sun.org.apache.xpath.internal.operations.Or 。

import org.h2.jdbcx.JdbcDataSource;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无用导入 - org.h2.jdbcx.JdbcDataSource 。


import java.io.File;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不应使用 '.*' 形式的导入 - java.sql.* 。

import java.util.ArrayList;
import java.util.List;

public class Sql {
Expand Down Expand Up @@ -62,6 +65,13 @@ public static class User {
String tel;
String address;

public User(Integer id, String name, String tel, String address) {
this.id = id;
this.name = name;
this.tel = tel;
this.address = address;
}

@Override
public String toString() {
return "User{" + "id=" + id + ", name='" + name + '\'' + ", tel='" + tel + '\'' + ", address='" + address + '\'' + '}';
Expand All @@ -82,7 +92,20 @@ public String toString() {
// | 2 |
// +-----+
public static int countUsersWhoHaveBoughtGoods(Connection databaseConnection, Integer goodsId) throws SQLException {
return 0;

String sql = "SELECT COUNT(distinct USER_ID) FROM `ORDER` where GOODS_ID =?";

try (PreparedStatement statement = databaseConnection.prepareStatement(sql)) {
//给第一个预编译的参数 传入 goodsId
statement.setInt(1, goodsId);
//获取查询结果集
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
//获取第一列的数据返回
return resultSet.getInt(1);
}
}
return -1;
}

/**
Expand All @@ -100,7 +123,23 @@ 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;
List<User> users = new ArrayList<>();

String sql = "select * from user order by id desc limit ? offset ?";
try(PreparedStatement statement = databaseConnection.prepareStatement(sql)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'try' 后应有空格。

statement.setInt(1,pageNum);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

',' 后应有空格。

statement.setInt(2,pageSize);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

',' 后应有空格。


ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Integer id = resultSet.getInt(1);
String name = resultSet.getString(2);
String tel = resultSet.getString(3);
String address = resultSet.getString(4);
users.add(new User(id,name,tel,address));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

',' 后应有空格。
',' 后应有空格。
',' 后应有空格。

}
}
return users;
}

// 商品及其营收
Expand All @@ -109,6 +148,12 @@ public static class GoodsAndGmv {
String goodsName; // 商品名
BigDecimal gmv; // 商品的所有销售额

public GoodsAndGmv(Integer goodsId, String goodsName, BigDecimal gmv) {
this.goodsId = goodsId;
this.goodsName = goodsName;
this.gmv = gmv;
}

@Override
public String toString() {
return "GoodsAndGmv{" + "goodsId=" + goodsId + ", goodsName='" + goodsName + '\'' + ", gmv=" + gmv + '}';
Expand All @@ -132,7 +177,22 @@ public String toString() {
// | 3 | goods3 | 20 |
// +----+--------+------+
public static List<GoodsAndGmv> getGoodsAndGmv(Connection databaseConnection) throws SQLException {
return null;
List<GoodsAndGmv> result = new ArrayList<>();
String sql = "select GOODS_ID,GOODS.NAME as NAME, SUM(GOODS_PRICE*GOODS_NUM) as GMV\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"from \"ORDER\" join GOODS on \"ORDER\".GOODS_ID= GOODS.ID\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"group by GOODS_ID order by GMV desc";
try (PreparedStatement statement = databaseConnection.prepareStatement(sql)) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
GoodsAndGmv temp = new GoodsAndGmv(
resultSet.getInt(1),
resultSet.getString(2),
resultSet.getBigDecimal(3)
);
result.add(temp);
}
}
return result;
}


Expand Down Expand Up @@ -170,7 +230,19 @@ public String toString() {
// | 6 | zhangsan | goods3 | 20 |
// +----------+-----------+------------+-------------+
public static List<Order> getInnerJoinOrders(Connection databaseConnection) throws SQLException {
return null;
List<Order> orders;
ResultSet resultSet;
String sql = "SELECT \"ORDER\".USER_ID, USER.NAME AS USER_NAME, GOODS.NAME AS GOODS_NAME, \"ORDER\".GOODS_NUM * \"ORDER\".GOODS_PRICE AS TOTAL_PRICE\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"FROM \"ORDER\"\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"JOIN USER ON \"ORDER\".USER_ID = USER.ID\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"JOIN GOODS ON \"ORDER\".GOODS_ID = GOODS.ID";

try (PreparedStatement statement = databaseConnection.prepareStatement(sql)) {
//获取结果集
resultSet = statement.executeQuery();
orders = fillingOrderList(resultSet);
}
return orders;
}

/**
Expand Down Expand Up @@ -198,9 +270,45 @@ public static List<Order> getInnerJoinOrders(Connection databaseConnection) thro
// | 8 | NULL | NULL | 60 |
// +----------+-----------+------------+-------------+
public static List<Order> getLeftJoinOrders(Connection databaseConnection) throws SQLException {
return null;
List<Order> orders;
String sql = "SELECT \"ORDER\".USER_ID, USER.NAME AS USER_NAME, GOODS.NAME AS GOODS_NAME, \"ORDER\".GOODS_NUM * \"ORDER\".GOODS_PRICE AS TOTAL_PRICE\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"FROM \"ORDER\"\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"LEFT JOIN USER ON \"ORDER\".USER_ID = USER.ID\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'+' 应另起一行。

"LEFT JOIN GOODS ON \"ORDER\".GOODS_ID = GOODS.ID";
ResultSet resultSet;
try (PreparedStatement statement = databaseConnection.prepareStatement(sql)) {
//获取结果集
resultSet = statement.executeQuery();
orders = fillingOrderList(resultSet);
}
return orders;
}

public static List<Order> fillingOrderList(ResultSet resultSet) {
List<Order> orderList = new ArrayList<>();
while (true) {
try {
if (!resultSet.next()) break;
Integer id = resultSet.getInt(1);
String user_name = resultSet.getString(2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

名称 'user_name' 必须匹配表达式: '^[a-z][a-zA-Z0-9]*$' 。

String goods_name = resultSet.getString(3);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

名称 'goods_name' 必须匹配表达式: '^[a-z][a-zA-Z0-9]*$' 。

BigDecimal price = resultSet.getBigDecimal(4);

Order order = new Order();
order.id = id;
order.userName = user_name;
order.goodsName = goods_name;
order.totalPrice = price;

orderList.add(order);
} catch (SQLException throwables) {
throwables.printStackTrace();
}

}

return orderList;
}
// 注意,运行这个方法之前,请先运行mvn initialize把测试数据灌入数据库
public static void main(String[] args) throws SQLException {
File projectDir = new File(System.getProperty("basedir", System.getProperty("user.dir")));
Expand Down