老师来康康
package common;
import java.sql.*;
public class DbUtils {
/**
* 创建新的数据库连接
* @return 新的Connection对象
* @throws ClassNotFoundException
* @throws SQLException
*/
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/imooc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true", "root", "123456");
return conn;
}
/**
* 关闭连接,释放资源
* @param rs 结果集对象
* @param stmt Statement对象
* @param conn Connection对象
*/
public static void closeConnection(ResultSet rs, Statement stmt, Connection conn){
try {
if(rs!=null){
rs.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if(stmt != null){
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn != null && !conn.isClosed() ) {//conn.isClosed() == false
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
==============================================
package goodsapp.command;
import common.DbUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class InsertCommand implements Command{
@Override
public void execute() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
String sql = "insert into goods(name,price,desp) values(?,?,?)";
conn = DbUtils.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "耳机");
pstmt.setFloat(2, 200);
pstmt.setString(3,"蓝牙耳机");
int cnt = pstmt.executeUpdate();
if(cnt==1){
System.out.println("添加成功");
pstmt = conn.prepareStatement("select * from goods");
rs = pstmt.executeQuery();
while (rs.next()){
Integer id = rs.getInt(1);
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 (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DbUtils.closeConnection(rs,pstmt,conn);
}
}
}
==============================================
package goodsapp.command;
import common.DbUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UpdateCommand implements Command {
@Override
public void execute() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
String sql = "update goods set price=? where name=?";
conn = DbUtils.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setFloat(1,1999);
pstmt.setString(2, "手机");
int cnt = pstmt.executeUpdate();
if(cnt!=0){
System.out.println("修改成功");
pstmt = conn.prepareStatement("select * from goods");
rs = pstmt.executeQuery();
while (rs.next()){
Integer id = rs.getInt(1);
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 (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DbUtils.closeConnection(rs,pstmt,conn);
}
}
}
==================================================
package goodsapp.command;
import common.DbUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DeleteCommand implements Command{
@Override
public void execute() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
String sql = "delete from goods where name=?";
conn = DbUtils.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "耳机");
int cnt = pstmt.executeUpdate();
if(cnt!=0){
System.out.println("删除成功");
pstmt = conn.prepareStatement("select * from goods");
rs = pstmt.executeQuery();
while (rs.next()){
Integer id = rs.getInt(1);
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 (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DbUtils.closeConnection(rs,pstmt,conn);
}
}
}
7
收起
正在回答
1回答
同学你好,已完成练习,不过有一个小建议,建议同学在反馈代码时,将测试类代码一并反馈便于老师测试代码。
祝学习愉快!
java工程师2020版
- 参与学习 人
- 提交作业 9410 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星