3-4 自由编程 作业
来源:3-4 自由编程
csm032
2020-04-22 19:44:31
java部分——
package com.imooc.jdbc.demo1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
public class LianXi_2_11 {
@Test
public void demo2() {
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest1?serverTimezone=Hongkong", "root", "123456");
stmt=conn.createStatement();
String sql="update goods set name='手机',price=1999.0,desp='小米黑色' where id=4";
int i=stmt.executeUpdate(sql);
if(i>0) {
System.out.println("修改成功");
sql="select * from goods";
rs=stmt.executeQuery(sql);
while(rs.next()) {
int id=rs.getInt("id");
String name=rs.getString("name");
float price=rs.getFloat("price");
String desp=rs.getString("desp");
System.out.println(id+" "+name+" "+price+" "+desp);
}
}else {
System.out.println("修改失败");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rs=null;
}
if(stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stmt=null;
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn=null;
}
}
}
}mysql部分——
create database jdbctest1;
use jdbctest1;
create table goods(
id int unsigned key auto_increment,
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,'变频空调');
select * from goods;
1回答
好帮手慕小脸
2020-04-23
同学写的不错哦,符合作业要求,继续加油哦!!!
如果我的回答解决了你的疑问,请采纳,祝学习愉快。
相似问题