自由编程打卡提交

来源:3-2 自由编程

TNHNM

2021-07-14 11:46:53

创建数据表

CREATE TABLE goods(
	id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT ,
	name VARCHAR(20) NOT NULL ,
	price FLOAT NOT NULL ,
	desp VARCHAR(30) NOT NULL
);

插入数据

INSERT INTO goods(name,price,desp) VALUES ("手机",2000,"黑色,存储容量32G"),("冰箱",1500,"银色,对开门"),("洗衣机",3000,"滚筒"),("空调",4000,"变频空调");

主方法

import java.lang.reflect.Field;
import java.sql.*;

public class Test {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/imooc?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicRetireval=true", "root", "123456");
            statement = connection.createStatement();
            resultSet = statement.executeQuery("select * from goods where price <3500");
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString("name");
                float price = resultSet.getInt(3);
                String desp = resultSet.getString("desp");
                System.out.println(id + " " + name + " " + " " + price + " " + desp);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }
}

写回答

1回答

好帮手慕小尤

2021-07-14

同学你好,已完成练习,棒棒哒!继续加油!

祝学习愉快!

0

0 学习 · 16556 问题

查看课程