老师帮忙瞅一下哈,谢谢!
package com.imooc.jdbc.exercise;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import org.junit.Test;
import com.imooc.jdbc.utils.JDBCUtils;
public class JDBCExercise3 {
@Test
// 1、首先将表格中的四条数据添加到数据库中
public void demo1() {
Connection conn = null;
Statement stmt = null;
String str[] = new String[4];
str[0] = "insert into goods values (null, '手机', 2000.0, '黑色,存储容量32G')";
str[1]= "insert into goods values (null, '冰箱', 1500.0, '银色,对开门')";
str[2] = "insert into goods values (null, '洗衣机', 3000.0, '滚筒')";
str[3] = "insert into goods values (null, '空调', 4000.0, '变频空调')";
for (int i = 0; i<str.length; i++) {
try {
// 获得连接:
conn = JDBCUtils.getConnection();
// 创建执行SQL语句的对象
stmt = conn.createStatement();
// 编写SQL:
String sql = str[i];
// 执行SQL:
int num = stmt.executeUpdate(sql);
if (num > 0) {
System.out.println("保存成功!"+(i+1));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源:
JDBCUtils.release(stmt, conn);
}
}
}
@Test
// 2、显示所有数据
public void demo2() {
Connection conn = null;
Statement stmt = null;
ResultSet resultSet = null;
try {
// 获得连接:
conn = JDBCUtils.getConnection();
// 创建执行SQL语句的对象
stmt = conn.createStatement();
// 编写SQL:
String sql = "select * from goods;";
// 执行SQL:
resultSet = stmt.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
float price = resultSet.getFloat("price");
String desp = resultSet.getString("desp");
System.out.println(id + " " + name + " " + price + " " + desp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源:
JDBCUtils.release(resultSet, stmt, conn);
}
}
@Test
// 3、查询name值为冰箱的数据并显示
public void demo3() {
Connection conn = null;
Statement stmt = null;
ResultSet resultSet = null;
try {
// 获得连接:
conn = JDBCUtils.getConnection();
// 创建执行SQL语句的对象
stmt = conn.createStatement();
// 编写SQL:
String sql = "select * from goods where name = '冰箱';";
// 执行SQL:
resultSet = stmt.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
float price = resultSet.getFloat("price");
String desp = resultSet.getString("desp");
System.out.println(id + " " + name + " " + price + " " + desp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源:
JDBCUtils.release(resultSet, stmt, conn);
}
}
@Test
// 4、将name值为手机的数据的price值改为5000
public void demo4() {
Connection conn = null;
Statement stmt = null;
try {
// 获得连接:
conn = JDBCUtils.getConnection();
// 创建执行SQL语句的对象
stmt = conn.createStatement();
// 编写SQL:
String sql = "update goods set price = 5000 where name = '手机'";
// 执行SQL:
int num = stmt.executeUpdate(sql);
if (num > 0) {
System.out.println("修改成功!");
demo2();
} else {
System.out.println("修改失败!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源:
JDBCUtils.release(stmt, conn);
}
}
@Test
// 5、删除name值为洗衣机的数据
public void demo5() {
Connection conn = null;
Statement stmt = null;
try {
// 获得连接:
conn = JDBCUtils.getConnection();
// 创建执行SQL语句的对象
stmt = conn.createStatement();
// 编写SQL:
String sql = "delete from goods where name = '洗衣机'";
// 执行SQL:
int num = stmt.executeUpdate(sql);
if (num > 0) {
System.out.println("删除成功!");
demo2();
} else {
System.out.println("删除失败!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源:
JDBCUtils.release(stmt, conn);
}
}
@Test
// 6、按价格升序排序显示所有数据
public void demo6() {
Connection conn = null;
Statement stmt = null;
try {
// 获得连接:
conn = JDBCUtils.getConnection();
// 创建执行SQL语句的对象
stmt = conn.createStatement();
// 编写SQL:
String sql = "select * from goods order by price asc;";
// 执行SQL:
ResultSet resultSet = stmt.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
float price = resultSet.getFloat("price");
String desp = resultSet.getString("desp");
System.out.println(id + " " + name + " " + price + " " + desp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放资源:
JDBCUtils.release(stmt, conn);
}
}
}5
收起
正在回答
1回答
同学完成的不错,很棒,加油。祝学习愉快~
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星