MySQL Demo



#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
    MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW row;
    char server[] = "localhost";
    char user[] = "root";
    char password[] = "root";
    char database[] = "mysql";
   
    conn = mysql_init(NULL);
   
    if (!mysql_real_connect(conn, server,user, password, database, 0, NULL, 0))
    {
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(1);
    }
   
    if (mysql_query(conn, "show tables"))
    {
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(1);
    }
   
    res = mysql_use_result(conn);
   
    printf("MySQL Tables in mysql database:\n");
   
    while ((row = mysql_fetch_row(res)) != NULL)
    {
        printf("%s \n", row[0]);
    }
   
    mysql_free_result(res);
    mysql_close(conn);
   
    printf("finish! \n");
    return 0;
}

### Spring Boot 和 MySQL 示例项目代码 以下是一个完整的 Spring Boot 与 MySQL 集成的示例项目,涵盖从依赖配置到数据库连接、实体类定义以及 REST API 的实现。 #### 1. 创建项目 通过访问 [http://start.spring.io/](http://start.spring.io/) 创建一个 Spring Boot 项目,并选择以下依赖项: - Spring Web - Spring Data JPA - MySQL Driver 创建完成后,下载并解压项目文件[^1]。 #### 2. 配置 `application.yml` 在 `src/main/resources` 下创建或修改 `application.yml` 文件,配置 MySQL 数据库连接信息: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/your_database_name?useSSL=false&serverTimezone=UTC username: root password: your_password driver-class-name: com.mysql.cj.jdbc.Driver jpa: hibernate: ddl-auto: update show-sql: true server: port: 9092 ``` 上述配置中,`your_database_name` 和 `your_password` 需要替换为实际的数据库名称和密码[^1]。 #### 3. 定义实体类 在 `src/main/java/com/example/demo` 下创建一个名为 `User.java` 的实体类: ```java package com.example.demo; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String email; // Getters and Setters public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } } ``` #### 4. 创建 Repository 接口 在同一包下创建 `UserRepository.java`,用于定义数据访问接口: ```java package com.example.demo; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Long> { } ``` #### 5. 创建 Controller 类 在同一包下创建 `UserController.java`,用于处理 HTTP 请求: ```java package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/api/users") public class UserController { @Autowired private UserRepository userRepository; @GetMapping public List<User> getAllUsers() { return userRepository.findAll(); } @PostMapping public User createUser(@RequestBody User user) { return userRepository.save(user); } } ``` #### 6. 启动类 确保项目的启动类 `DemoApplication.java` 已正确配置: ```java package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` #### 7. 测试运行 运行项目后,可以通过以下方式测试 API: - 添加用户:发送 POST 请求到 `http://localhost:9092/api/users`,请求体为 JSON 格式: ```json { "name": "John Doe", "email": "john.doe@example.com" } ``` - 获取所有用户:发送 GET 请求到 `http://localhost:9092/api/users`。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值