import java.sql.*;
public class case2 {
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/student";
static final String USER = "root"; // 替换为你的用户名
static final String PASS = "123456"; // 替换为你的密码
public static void main(String[] args) {
Connection conn = null;
try {
// 加载 JDBC 驱动
Class.forName(JDBC_DRIVER);
// 建立连接
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// 创建 tb_account 表
String createTableSQL = "CREATE TABLE IF NOT EXISTS `tb_account` ("
+ " `user_id` CHAR(8) NOT NULL,"
+ " `user_name` CHAR(10) NOT NULL,"
+ " `password` CHAR(10) NOT NULL DEFAULT '123456',"
+ " PRIMARY KEY (`user_id`)"
+ ")";
try (Statement statement = conn.createStatement()) {
statement.executeUpdate(createTableSQL);
System.out.println("Table 'tb_account' created successfully.");
}
// 插入数据到 tb_account 表
String insertSQL = "INSERT INTO `tb_account` (`user_id`, `user_name`)"
+ " SELECT `stu_id`, `stu_name` FROM `tb_student`";
try (Statement statement = conn.createStatement()) {
int rowsAffected = statement.executeUpdate(insertSQL);
if (rowsAffected > 0) {
System.out.println("Data inserted successfully into 'tb_account'.");
}
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
内容概要 JDBC应用实验是一个面向编程和数据库管理领域的教学项目,旨在教授学生如何使用Java数据库连接(JDBC)API与MySQL数据库进行交互。项目将涵盖以下关键内容: JDBC基础:介绍JDBC的概念、作用以及如何在Java项目中集成JDBC。 常用类和接口:详细解释JDBC中的核心类和接口,如Connection, Statement, PreparedStatement, ResultSet等。 数据库连接:演示如何建立与MySQL数据库的连接。 SQL语句执行:教授如何通过JDBC执行查询和更新操作。 异常处理:介绍JDBC操作中可能遇到的异常及其处理方法。 事务管理:讲解如何在JDBC中处理数据库事务。 连接池的使用:可选,介绍如何使用连接池提高数据库操作的效率。 使用人群 计算机科学与技术专业的学生:需要了解数据库编程基础。 软件开发人员:希望扩展其技能集,包括数据库交互能力。 数据库管理员:想要学习如何通过编程方式管理数据库。 自学者:对Java编程和数据库操作感兴趣的个人。 使用场景 教育环境:作为大学课程或在线教程的一部分,教授数据库编程。 开发环境:在软件
资源推荐
资源详情
资源评论
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 4 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/aee2703d2b584eeaba5381102d5105fd_weixin_49816293.jpg!1)
编程梦想家(大学生版)
- 粉丝: 508
- 资源: 4
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)