老师请检查

来源:4-4 自由编程

慕工程8205364

2021-05-29 22:01:04

package com.imooc.jdbc_zy.sx;

import com.imooc.jdbc_zy.dbutils.Dbutils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class CzConnection {
/**
    * 查询所有的商品信息
    */
   public void SelectConnection() {
Connection conn = null;
       PreparedStatement pstmt = null;
       ResultSet rs = null;
       try {
conn = Dbutils.getConnection();
           String sql = "select * from goods";
           pstmt = conn.prepareStatement(sql);
           rs = pstmt.executeQuery();
           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);
           }
} catch (ClassNotFoundException e) {
e.printStackTrace();
       } catch (SQLException throwables) {
throwables.printStackTrace();
       } finally {
Dbutils.closedConnection(rs, pstmt, conn);
       }
}
/**
    * 添加商品信息
    */
   public void InsertConnection() {
System.out.println("请输入商品名称:");
       Scanner sc = new Scanner(System.in);
       String name = sc.next();
       System.out.println("请输入商品价格:");
       Float price = sc.nextFloat();
       System.out.println("请输入商品描述:");
       String desp = sc.next();
       Connection conn = null;
       PreparedStatement pstmt = null;
       try {
conn = Dbutils.getConnection();
           String sql = "insert into goods(name,price,desp) values (?,?,?)";
           pstmt = conn.prepareStatement(sql);
           pstmt.setString(1, name);
           pstmt.setFloat(2, price);
           pstmt.setString(3, desp);
           int i = pstmt.executeUpdate();
           System.out.println("商品信息添加成功!!");
       } catch (ClassNotFoundException e) {
e.printStackTrace();
       } catch (SQLException throwables) {
throwables.printStackTrace();
       } finally {
Dbutils.closedConnection(null, pstmt, conn);
       }
}

/**
    * 删除商品信息
    */
   public void DeleteConnection() {
System.out.println("请输入商品名称:");
       Scanner sc = new Scanner(System.in);
       String name = sc.next();
       Connection conn = null;
       PreparedStatement pstmt = null;
       try {
conn = Dbutils.getConnection();
           String sql = "delete from goods where name=?";
           pstmt = conn.prepareStatement(sql);
           pstmt.setString(1, name);
           int i = pstmt.executeUpdate();
           if (i == 0) {
System.out.println("该商品不存在!!");
           } else {
System.out.println("商品删除成功!!");
           }
} catch (ClassNotFoundException e) {
e.printStackTrace();
       } catch (SQLException throwables) {
throwables.printStackTrace();
       } finally {
Dbutils.closedConnection(null, pstmt, conn);
       }
}

/**
    * 修改商品信息
    */
   public void UpdateConnection() {
System.out.println("请输入商品名称:");
       Scanner sc = new Scanner(System.in);
       String name = sc.next();
       System.out.println("请输入商品价格:");
       Float price = sc.nextFloat();
       Connection conn = null;
       PreparedStatement pstmt = null;
       try {
conn = Dbutils.getConnection();
           String sql = "update goods set price=? where name=?";
           pstmt = conn.prepareStatement(sql);
           pstmt.setFloat(1, price);
           pstmt.setString(2, name);
           int i = pstmt.executeUpdate();
           if (i == 0) {
System.out.println("当前商品不存在!!");
           } else {
System.out.println("商品信息修改成功!!");
           }
} catch (ClassNotFoundException e) {
e.printStackTrace();
       } catch (SQLException throwables) {
throwables.printStackTrace();
       } finally {
Dbutils.closedConnection(null, pstmt, conn);
       }
}

public void cd() {
System.out.println("1.添加商品信息");
       System.out.println("2.修改商品信息");
       System.out.println("3.删除商品信息");
       System.out.println("4.查询所有的商品信息");
       System.out.println("0.退出程序");
   }
public static void main(String[] args) {
CzConnection c = new CzConnection();
       Scanner sc = new Scanner(System.in);
       while (true) {
c.cd();
           System.out.println("请选择操作:");
           int cz = sc.nextInt();
           if (cz == 0) {
System.out.println("程序已终止!!");
               break;
           }
switch (cz) {
case 1:
c.InsertConnection();
                   break;
               case 2:
c.UpdateConnection();
                   break;
               case 3:
c.DeleteConnection();
                   break;
               case 4:
c.SelectConnection();
                   break;
               default:
System.out.println("无效操作!!");
           }
}
}
}





package com.imooc.jdbc_zy.dbutils;

import java.sql.*;

public class Dbutils {
/**
    * 创建一个新的数据库连接
    * @return 返回一个connection对象
    * @throws ClassNotFoundException
    * @throws SQLException
    */
   public static Connection getConnection() throws ClassNotFoundException, SQLException {
//        1.加载并注册jdbc驱动
       Class.forName("com.mysql.cj.jdbc.Driver");
//        2.创建数据库连接
       Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/imooc?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&allowPublickeyRetrieval=true",
               "root",
               "991108"
       );
       return conn;

   }

/**
    * 关闭连接,释放资源
    * @param rs 结果集对象
    * @param stmt 执行sql语句的Statement对象
    * @param conn jdbc和数据库的通信对象
    */
   public static void closedConnection(ResultSet rs, Statement stmt, Connection conn) {
try {
if (rs != null) {
rs.close();
           }
if (stmt != null) {
stmt.close();
           }
if (conn != null && !conn.isClosed()) {
conn.close();
           }
} catch (SQLException throwables) {
throwables.printStackTrace();
       }

}
}


写回答

1回答

好帮手慕小班

2021-05-30

同学的代码完成的不错,继续加油。祝学习愉快!
0

0 学习 · 16556 问题

查看课程