2-11 自由编程
来源:2-11 自由编程
JakePrim
2020-02-22 21:14:22
CREATE TABLE IF NOT EXISTS goods( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20) NOT NULL, price FLOAT NOT NULL, desp VARCHAR(30) NOT NULL ); INSERT goods(name,price,desp) VALUES('手机',2000.0,'黑色内存容量 32G'), ('冰箱',1500.0,'银色对开门'), ('洗衣机',3000.0,'滚筒'), ('空调',4000,'变频空调');
public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/king", "root", "root"); Statement statement = connection.createStatement(); String sql = "SELECT * FROM goods WHERE price < 3500"; ResultSet set = statement.executeQuery(sql); while (set.next()) { int id = set.getInt("id"); String name = set.getString("name"); float price = set.getFloat("price"); String desp = set.getString("desp"); System.out.println(id+" "+name+" "+price+" "+desp); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
1回答
你好!程序运行效果正确,符合作业要求,完成得非常好!建议在代码中添加更多的注释,便于后期维护。继续加油!祝学习愉快!
相似问题