查询数据显示的是修改和添加的数据

查询数据显示的是修改和添加的数据

package com.imooc.jdbc.demo1;


import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.Statement;


import org.junit.Test;


import com.imooc.jdbc.utils.JDBCUtils;


public class goods {

/**

* 1、首先将表格中的四条数据添加到数据库中

*/

@Test

public void demo() {

Connection conn = null;

Statement stmt = null;

try {

// 获得连接

conn = JDBCUtils.getConnection();

//创建执行SQL语句的对象

stmt = conn.createStatement();

//编写SQL

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

int i = stmt.executeUpdate(sql);

if(i > 0) {

System.out.println("添加成功!");

}


} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


/**

* 2、显示所有数据

*/

@Test

public void demo2() {

Connection conn = null;

Statement stmt = null;

ResultSet resultSet = null;

try {

conn = JDBCUtils.getConnection();

stmt = conn.createStatement();

String sql = "select * from goods";

resultSet = stmt.executeQuery(sql);

while(resultSet.next()) {

int id = resultSet.getInt("id");

String name = resultSet.getString("NAME");

String price = resultSet.getString("price");

String desp = resultSet.getString("desp");

System.out.println(id+"           "+name+"           "+price+"           "+desp);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

JDBCUtils.release(resultSet, stmt, conn);

}

}

/**

* 3、查询name值为冰箱的数据并显示

*/

@Test

public void demo3() {

Connection conn = null;

Statement stmt = null;

ResultSet resultSet = null;

try {

conn = JDBCUtils.getConnection();

stmt = conn.createStatement();

String sql = "select * from goods where NAME='冰箱'";

resultSet = stmt.executeQuery(sql);

while(resultSet.next()) {

int id = resultSet.getInt("id");

String name = resultSet.getString("NAME");

String price = resultSet.getString("price");

String desp = resultSet.getString("desp");

System.out.println(id+"           "+name+"           "+price+"           "+desp);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

JDBCUtils.release(resultSet, stmt, conn);

}

}

/**

*  4、将name值为手机的数据的price值改为5000

*/

@Test

public void demo4() {

Connection conn = null;

Statement stmt = null;

ResultSet resultSet = null;

try {

conn = JDBCUtils.getConnection();

stmt = conn.createStatement();

String sql = "update goods set price=5000 where NAME='手机'";

int i = stmt.executeUpdate(sql);

if(i > 0) {

System.out.println("修改成功!");

}

sql = "select * from goods";

resultSet = stmt.executeQuery(sql);

while(resultSet.next()) {

int id = resultSet.getInt("id");

String name = resultSet.getString("NAME");

String price = resultSet.getString("price");

String desp = resultSet.getString("desp");

System.out.println(id+"           "+name+"           "+price+"           "+desp);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

JDBCUtils.release(resultSet, stmt, conn);

}

}

/**

*  5、删除name值为洗衣机的数据

*/

@Test

public void demo5() {

Connection conn = null;

Statement stmt = null;

ResultSet resultSet = null;

try {

conn = JDBCUtils.getConnection();

stmt = conn.createStatement();

String sql = "select * from goods where NAME='洗衣机'";

resultSet = stmt.executeQuery(sql);

while(resultSet.next()) {

int id = resultSet.getInt("id");

String name = resultSet.getString("NAME");

String price = resultSet.getString("price");

String desp = resultSet.getString("desp");

System.out.println(id+"           "+name+"           "+price+"           "+desp);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

JDBCUtils.release(resultSet, stmt, conn);

}

}

/**

*  6、按价格升序排序显示所有数据

*/

@Test

public void demo6() {

Connection conn = null;

Statement stmt = null;

ResultSet resultSet = null;

try {

conn = JDBCUtils.getConnection();

stmt = conn.createStatement();

String sql = "select * from goods order by price ASC";

resultSet = stmt.executeQuery(sql);

while(resultSet.next()) {

int id = resultSet.getInt("id");

String name = resultSet.getString("NAME");

String price = resultSet.getString("price");

String desp = resultSet.getString("desp");

System.out.println(id+"           "+name+"           "+price+"           "+desp);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

JDBCUtils.release(resultSet, stmt, conn);

}

}

}

http://img1.sycdn.imooc.com//climg/5e664d48084a028210070392.jpg不管是demo(),demo2(),demo3()等哪个方法显示的结果都是上图

正在回答 回答被采纳积分+1

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

2回答
好帮手慕酷酷 2020-03-10 15:44:06

同学你好,老师测试同学的代码是没有问题的,如:

http://img1.sycdn.imooc.com//climg/5e67449809bc7e4204900415.jpg

建议同学在选中demo2方法时,为蓝色时,进行右键demo2的位置进行运行。也就只运行demo2方法了。或者同学重启一下eclipse试试。

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

好帮手慕酷酷 2020-03-10 11:57:08

同学你好,这里应该是同学的运行代码时,直接在类上右键运行的原因,这里当同学想要测试某一个方法时,应该选中方法名,右键运行。具体如下:

http://img1.sycdn.imooc.com//climg/5e670ff109738fb606590465.jpg

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

  • 提问者 慕的地2082093 #1
    老师,我是这样运行的,但是运行的结果不对,比如运行demo2(),结果应该是显示数据,但是显示的是添加成功,然后显示了数据,并出现修改成功,显示了修改成功后显示的数据又增加了,结果是上面我截的图
    2020-03-10 12:08:31
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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