老师帮忙瞅一下哈,谢谢!

老师帮忙瞅一下哈,谢谢!

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);
		}
	}
}


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学完成的不错,很棒,加油。祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师