查询数据显示的是修改和添加的数据
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);
}
}
}
不管是demo(),demo2(),demo3()等哪个方法显示的结果都是上图
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星