找回密码
 立即注册
搜索
热搜: Python Java C Html PHP
查看: 683|回复: 0

编程 JDBC

[复制链接]

42

主题

10

回帖

8

积分

管理员

积分
8

原始用户

发表于 2026-3-16 15:16:53 | 显示全部楼层 |阅读模式

试题3、JDBC的基本操作

本题分值:45
考核时间:90分钟
考核方式:实操
考核内容:使用JDBC连接数据库,并进行增删改查基本操作。本题涉及的表有两张:
托运公司表(com):包含托运公司编号,托运公司名称,电话,地址四个字段。
订单表(t_order):包含订单号,托运公司编号,商品数量,运输方式四个字段。
托运公司表:
id
name
adress
1
顺丰速递
上海
2
申通速递
武汉
3
中远
北京

订单表:
pid
id
count
way
001
1
100
海运
002
1
50
空运
003
2
500
陆路运输
004
3
30
空运

以下sql语句对表进行了创建,添加了上面表格中的数据:
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for com
-- ----------------------------
DROP TABLE IF EXISTS com;
CREATE TABLE com (
   id int(11) NOT NULL,
   name varchar(20) DEFAULT NULL,
   adress varchar(20) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of com
-- ----------------------------
INSERT INTO com VALUES ('1', '顺丰速递', '上海');
INSERT INTO com VALUES ('2', '申通速递', '武汉');
INSERT INTO com VALUES ('3', '中远', '北京');
-- ----------------------------
-- Table structure for t_order
-- ----------------------------
DROP TABLE IF EXISTS t_order;
CREATE TABLE t_order (
  pid varchar(10) NOT NULL,
  id int(11) NOT NULL,
  count int(11) DEFAULT NULL,
way varchar(20) DEFAULT NULL,
  PRIMARY KEY (pid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_order
-- ----------------------------
INSERT INTO t_order VALUES ('001', '1', '100', '海运');
INSERT INTO t_order VALUES ('002', '1', '50', '空运');
INSERT INTO t_order VALUES ('003', '2', '500', '陆路运输');
INSERT INTO t_order VALUES ('004', '3', '30', '空运');


在上面sql语句的基础上请实现以下操作:
(1)创建log数据库,导入log.sql脚本;
(2)使用JDBC连接log数据库;
(3)使用JDBC向com托运公司表中插入id为4,name为圆通,adress为广州的一条记录;
(4)使用JDBC查询上海的运输公司名称;
(5)使用JDBC将com表中id为3的快递公司地址改为深圳;
(6)使用JDBC删除id为1的托运公司记录。

  1. import java.sql.*;
  2. public class JdbcLogDBOperation {
  3.     private static final String DB_URL = "jdbc:mysql://199.7.140.3:3306/tester?characterEncoding=utf-8";
  4.     private static final String DB_USER = "tester";
  5.     private static final String DB_PASSWORD = "Taoch185152972#";

  6.     public static Connection getConnection() throws SQLException {
  7.         try {
  8.             Class.forName("com.mysql.jdbc.Driver");
  9.         } catch (ClassNotFoundException e) {
  10.             e.printStackTrace();
  11.         }
  12.         return DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
  13.     }

  14.     public static void insertCom() {
  15.         String sql = "INSERT INTO com(id, name, adress) VALUES (?,?,?)";
  16.         try (Connection conn = getConnection();
  17.              PreparedStatement pstmt = conn.prepareStatement(sql)) {
  18.             // 设置参数
  19.             pstmt.setInt(1, 4);
  20.             pstmt.setString(2, "圆通");
  21.             pstmt.setString(3, "广州");
  22.             // 执行插入
  23.             int affectedRows = pstmt.executeUpdate();
  24.             if (affectedRows > 0) {
  25.                 System.out.println("插入成功");
  26.             }
  27.         } catch (SQLException e) {
  28.             e.printStackTrace();
  29.         }
  30.     }

  31.     public static void queryComByAddress() {
  32.         String sql = "SELECT name FROM com WHERE adress = ?";
  33.         try (Connection conn = getConnection();
  34.              PreparedStatement pstmt = conn.prepareStatement(sql)) {

  35.             pstmt.setString(1, "上海");

  36.             ResultSet rs = pstmt.executeQuery();

  37.             System.out.println("上海的运输公司名称:");
  38.             while (rs.next()) {
  39.                 String name = rs.getString("name");
  40.                 System.out.println(name);
  41.             }
  42.         } catch (SQLException e) {
  43.             e.printStackTrace();
  44.         }
  45.     }

  46.     public static void updateComAddress() {
  47.         String sql = "UPDATE com SET adress = ? WHERE id = ?";
  48.         try (Connection conn = getConnection();
  49.              PreparedStatement pstmt = conn.prepareStatement(sql)) {

  50.             pstmt.setString(1, "深圳");
  51.             pstmt.setInt(2, 3);

  52.             int affectedRows = pstmt.executeUpdate();
  53.             if (affectedRows > 0) {
  54.                 System.out.println("修改成功");
  55.             }
  56.         } catch (SQLException e) {
  57.             e.printStackTrace();
  58.         }
  59.     }

  60.     public static void deleteCom() {
  61.         String deleteOrderSql = "DELETE FROM t_order WHERE id = ?";

  62.         try (Connection conn = getConnection();
  63.              PreparedStatement pstmt = conn.prepareStatement(deleteOrderSql)) {

  64.             pstmt.setInt(1, 1);

  65.             int affectedRows = pstmt.executeUpdate();
  66.             if (affectedRows > 0) {
  67.                 System.out.println("删除成功");
  68.             }
  69.         } catch (SQLException e) {
  70.             e.printStackTrace();
  71.         }
  72.     }

  73.     public static void main(String[] args) {
  74.         insertCom();
  75.         queryComByAddress();
  76.         updateComAddress();
  77.         deleteCom();
  78.     }
  79. }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|HELIXABYSS

GMT+8, 2026-7-21 21:18 , Processed in 0.084188 second(s), 19 queries .

Powered by Discuz! X3.5

© 2024-2026 HELIXABYSS

快速回复 返回顶部 返回列表