用java代码创建名为学号+姓名首字母的数据表,字段包括id,name,age,其中应有个人信息,使用JDBC知识,根据id更新姓名,更新后的姓名中需有学号,如202220101.张三 20220101java代码
时间: 2024-04-21 21:26:14 浏览: 28
好的,这是一个比较典型的JDBC操作,可以按照以下步骤实现:
1. 导入JDBC相关的jar包,比如mysql-connector-java。
2. 建立数据库连接,这里假设数据库名为test,用户名为root,密码为123456,连接地址为localhost:3306。
```
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, user, password);
```
3. 创建数据表,需要使用CREATE TABLE语句,这里可以使用String.format方法构建SQL语句。
```
String tableName = "20220101" + name.substring(0, 1);
String createTableSql = String.format(
"CREATE TABLE %s (id INT PRIMARY KEY, name VARCHAR(50), age INT)", tableName);
Statement stmt = conn.createStatement();
stmt.executeUpdate(createTableSql);
```
4. 插入个人信息,使用INSERT INTO语句。
```
String insertSql = String.format(
"INSERT INTO %s (id, name, age) VALUES (%d, '%s', %d)",
tableName, id, name, age);
stmt.executeUpdate(insertSql);
```
5. 更新姓名,使用UPDATE语句。
```
String updateSql = String.format(
"UPDATE %s SET name='%s' WHERE id=%d", tableName, newName, id);
stmt.executeUpdate(updateSql);
```
6. 关闭数据库连接。
```
stmt.close();
conn.close();
```
完整的代码实现如下:
```
import java.sql.*;
public class Main {
public static void main(String[] args) throws Exception {
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, user, password);
int id = 202220101;
String name = "张三";
int age = 20;
String tableName = "20220101" + name.substring(0, 1);
String createTableSql = String.format(
"CREATE TABLE %s (id INT PRIMARY KEY, name VARCHAR(50), age INT)", tableName);
Statement stmt = conn.createStatement();
stmt.executeUpdate(createTableSql);
String insertSql = String.format(
"INSERT INTO %s (id, name, age) VALUES (%d, '%s', %d)",
tableName, id, name, age);
stmt.executeUpdate(insertSql);
String newName = "20220101" + "李四";
String updateSql = String.format(
"UPDATE %s SET name='%s' WHERE id=%d", tableName, newName, id);
stmt.executeUpdate(updateSql);
stmt.close();
conn.close();
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)