• <small id='Jkf5o'></small><noframes id='Jkf5o'>

      • <bdo id='Jkf5o'></bdo><ul id='Jkf5o'></ul>
      <i id='Jkf5o'><tr id='Jkf5o'><dt id='Jkf5o'><q id='Jkf5o'><span id='Jkf5o'><b id='Jkf5o'><form id='Jkf5o'><ins id='Jkf5o'></ins><ul id='Jkf5o'></ul><sub id='Jkf5o'></sub></form><legend id='Jkf5o'></legend><bdo id='Jkf5o'><pre id='Jkf5o'><center id='Jkf5o'></center></pre></bdo></b><th id='Jkf5o'></th></span></q></dt></tr></i><div id='Jkf5o'><tfoot id='Jkf5o'></tfoot><dl id='Jkf5o'><fieldset id='Jkf5o'></fieldset></dl></div>

        <tfoot id='Jkf5o'></tfoot>

        <legend id='Jkf5o'><style id='Jkf5o'><dir id='Jkf5o'><q id='Jkf5o'></q></dir></style></legend>

        IDEA 链接Mysql数据库并执行查询操作的完整代码

        下面我将介绍如何使用IntelliJ IDEA链接MySQL数据库并执行查询操作,步骤如下:
        <tfoot id='vgHAA'></tfoot>
          <tbody id='vgHAA'></tbody>

            1. <small id='vgHAA'></small><noframes id='vgHAA'>

                <bdo id='vgHAA'></bdo><ul id='vgHAA'></ul>
              • <i id='vgHAA'><tr id='vgHAA'><dt id='vgHAA'><q id='vgHAA'><span id='vgHAA'><b id='vgHAA'><form id='vgHAA'><ins id='vgHAA'></ins><ul id='vgHAA'></ul><sub id='vgHAA'></sub></form><legend id='vgHAA'></legend><bdo id='vgHAA'><pre id='vgHAA'><center id='vgHAA'></center></pre></bdo></b><th id='vgHAA'></th></span></q></dt></tr></i><div id='vgHAA'><tfoot id='vgHAA'></tfoot><dl id='vgHAA'><fieldset id='vgHAA'></fieldset></dl></div>
                • <legend id='vgHAA'><style id='vgHAA'><dir id='vgHAA'><q id='vgHAA'></q></dir></style></legend>

                • 下面我将介绍如何使用IntelliJ IDEA链接MySQL数据库并执行查询操作,步骤如下:

                  环境准备:

                  1. 确保你已经安装了Java SDK和IntelliJ IDEA开发环境。
                  2. 确保已经安装了mysql数据库,并且知道数据库的地址、端口、账号和密码。

                  步骤:

                  1. 在IntelliJ IDEA中创建一个Java项目。

                  2. 导入 MySQL JDBC 驱动,这里我使用的是mysql-connector-java-8.0.27.jar,可以在 https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.27 下载并导入。

                  直接在项目根目录下创建libs文件夹,把mysql-connector-java-8.0.27.jar复制到此目录下。

                  1. 在 IntelliJ IDEA 中添加 MySQL JDBC 驱动依赖:

                  打开File --> Project Structure --> Modules,选择Dependencies选项卡,然后点击“+”号,添加JARs or directories,选择刚才复制的mysql-connector-java-8.0.27.jar。

                  在这之后还需配置 CLASSPATH,右键点击项目的 src 目录,选择“Mark Directory As” --> “Sources Root”。

                  1. 在代码中链接 MySQL 数据库,这里我使用的是 com.mysql.cj.jdbc.Driver:
                  public static void main(String[] args) throws SQLException {
                      //1.注册数据库驱动
                      try {
                          Class.forName("com.mysql.cj.jdbc.Driver");
                      } catch (ClassNotFoundException e) {
                          e.printStackTrace();
                      }
                      //2.获得数据库连接
                      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT", "root", "password");
                  
                      //3.获得执行SQL语句
                      Statement stmt = conn.createStatement();
                  
                      //4.得到结果集
                      String sql = "select * from user";
                      ResultSet rs = stmt.executeQuery(sql);
                  
                      //5.循环输出结果集
                      while(rs.next()) {
                          int id = rs.getInt("id");
                          String name = rs.getString("name");
                          int age = rs.getInt("age");
                          System.out.println(id + "\t" + name + "\t" + age);
                      }
                  
                      //6.释放资源
                      stmt.close();
                      conn.close();
                  }
                  

                  在代码中的 URL、username 和 password 需要替换为你自己的 MySQL 数据库地址、账户和密码。

                  示例1:执行SQL命令创建表

                  public static void createTable() throws SQLException {
                      //1.注册数据库驱动
                      try {
                          Class.forName("com.mysql.cj.jdbc.Driver");
                      } catch (ClassNotFoundException e) {
                          e.printStackTrace();
                      }
                      //2.获得数据库连接
                      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT", "root", "password");
                  
                      //3.获得执行SQL语句的Statement对象
                      Statement stmt = conn.createStatement();
                  
                      //4.执行SQL语句,创建user表
                      String sql = "CREATE TABLE user(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20), age INT)";
                      stmt.execute(sql);
                  
                      //5.释放资源
                      stmt.close();
                      conn.close();
                  }
                  

                  示例2:执行SQL命令插入数据

                  public static void insertData() throws SQLException {
                      //1.注册数据库驱动
                      try {
                          Class.forName("com.mysql.cj.jdbc.Driver");
                      } catch (ClassNotFoundException e) {
                          e.printStackTrace();
                      }
                      //2.获得数据库连接
                      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT", "root", "password");
                  
                      //3.获得执行SQL语句的Statement对象
                      Statement stmt = conn.createStatement();
                  
                      //4.执行SQL语句,插入数据
                      String sql = "INSERT INTO user(name, age) VALUES('小明', 16)";
                      stmt.execute(sql);
                  
                      //5.释放资源
                      stmt.close();
                      conn.close();
                  }
                  

                  这里只是展示了如何连接 MySQL 数据库并执行查询操作,更多关于Java连接MySQL的操作可以查看MySQL JDBC驱动 API 文档。

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  下面是针对PostgreSQL中的权限问题的完整攻略。
                  MySQL是一种流行的关系型数据库系统,它提供了多种时间类型和模式,用于存储和处理时间数据。本文将详细介绍MySQL时间类型和模式的详细攻略。
                  首先在官网下载CentOS7镜像,并在VMware虚拟机中新建一台CentOS7虚拟机,将镜像挂载到虚拟机中并启动。
                  首先,当我们使用Spring Boot开发项目时,可能会遇到Error starting ApplicationContext错误,一般这种错误是由于配置文件、依赖包或者代码逻辑等原因引起的。下面我将提供一条包含两条详细示例说明的完整攻略,用来解决上述问题。
                  下面我将详细讲解如何为PostgreSQL数据库中的用户授予权限和撤销权限,包括两个实例。
                  MySQL中出现lock wait timeout exceeded问题的原因是由于两个或多个事物同时请求相同的资源造成的,并且在某一时刻至少一个事务无法获取资源,超过了MySQL默认的等待时间,从而导致事务失败。这种问题的出现会极大地影响数据库的性能和并发能力。
                  <tfoot id='vf8pZ'></tfoot>

                    • <bdo id='vf8pZ'></bdo><ul id='vf8pZ'></ul>
                    • <small id='vf8pZ'></small><noframes id='vf8pZ'>

                      <i id='vf8pZ'><tr id='vf8pZ'><dt id='vf8pZ'><q id='vf8pZ'><span id='vf8pZ'><b id='vf8pZ'><form id='vf8pZ'><ins id='vf8pZ'></ins><ul id='vf8pZ'></ul><sub id='vf8pZ'></sub></form><legend id='vf8pZ'></legend><bdo id='vf8pZ'><pre id='vf8pZ'><center id='vf8pZ'></center></pre></bdo></b><th id='vf8pZ'></th></span></q></dt></tr></i><div id='vf8pZ'><tfoot id='vf8pZ'></tfoot><dl id='vf8pZ'><fieldset id='vf8pZ'></fieldset></dl></div>
                        • <legend id='vf8pZ'><style id='vf8pZ'><dir id='vf8pZ'><q id='vf8pZ'></q></dir></style></legend>

                              <tbody id='vf8pZ'></tbody>