Java实现基本MySQL连接 - 数据的基本操作

import java.sql.*;

public class Main {
    // MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
    //static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    //static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB";

    // MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&useServerPrepStmts=true";


    // 数据库的用户名与密码,需要根据自己的设置
    static final String USER = "root";
    static final String PASS = "root";

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        String sql = "";
        try {
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);
            // 打开链接
            connection = DriverManager.getConnection(DB_URL, USER, PASS);
            // 实例化Statement对象
            statement = connection.createStatement();
            // 删除数据库
            statement.execute("DROP DATABASE IF EXISTS `services`;");
            // 新建数据库
            statement.execute("CREATE DATABASE `services` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';");
            // 删除数据表
            statement.execute("DROP TABLE IF EXISTS `services`.`websites`;");
            // 新建数据表
            statement.execute("CREATE TABLE `services`.`websites`  (\n" +
                    "  `id` int(2) NOT NULL COMMENT 'id',\n" +
                    "  `name` varchar(255) NOT NULL,\n" +
                    "  `url` varchar(255) NOT NULL,\n" +
                    "  PRIMARY KEY (`id`),\n" +
                    "  UNIQUE INDEX(`id`)\n" +
                    ") ENGINE = InnoDB;");
            // 插入记录
            statement.execute("INSERT INTO `services`.`websites`(`id`,`name`,`url`) VALUES ('1','百度','https://www.baidu.com/');");
            statement.execute("INSERT INTO `services`.`websites`(`id`,`name`,`url`) VALUES ('2','博客园','https://www.cnblogs.com/');");
            // 查询记录
            statement.execute("SELECT `id`,`name`,`url` FROM `services`.`websites`;");
            resultSet = statement.getResultSet();
            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
            int cols = resultSetMetaData.getColumnCount();
            // 输出所有字段名
            for (int i = 1; i <= cols; i++) {
                System.out.println(resultSetMetaData.getColumnName(i));
            }
            // 输出所有数据
            while (resultSet.next()) {
                System.out.println(resultSet.getString(1));
                System.out.println(resultSet.getString(2));
                System.out.println(resultSet.getString(3));
            }
            resultSet.close();
            statement.close();
            connection.close();
        } catch (SQLException se) {
            // 处理 JDBC 错误
            se.printStackTrace();
        } catch (Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        } finally {

        }
        System.out.println("Goodbye!");
    }
}
posted @ 2022-12-20 19:31  我不是萌新  阅读(28)  评论(0编辑  收藏  举报