各位用户为了找寻关于在IDEA的maven项目中连接并使用MySQL8.0的方法教程的资料费劲了很多周折。这里教程网为您整理了关于在IDEA的maven项目中连接并使用MySQL8.0的方法教程的相关资料,仅供查阅,以下为您介绍关于在IDEA的maven项目中连接并使用MySQL8.0的方法教程的详细内容

首先看一下我的基本的开发环境:

操作系统:MacOS 10.13.5编辑器:IDEA 2018.3其他:MySQL8.0.15、Maven 3.3.9、JDK 1.8

好,下面就正式开始:

第一步:在IDEA中新建一个maven项目

1.使用骨架创建maven项目,此处选择:maven-archetype-quickstart

2.填入GroupId和ArtifactId

3.第一个选中maven安装的文件夹,第二个选中maven安装文件夹中的conf/settings.xml,第三个如果settings.xml中配置了localRepository,则会自动填入,若没有则会显示默认的本地仓库

4.点击Finish即可成功创建maven项目

第二步:配置pom.xml

在pom.xml中的标签内加入要用到的jar包在仓库中的坐标

1.dom4j的jar包坐标

? 1 2 3 4 5 <dependency>  <groupId>org.dom4j</groupId>  <artifactId>dom4j</artifactId>  <version>2.1.1</version> </dependency>

 

2.mysql的jar包坐标

? 1 2 3 4 5 6 <dependency>   <groupId>mysql</groupId>   <artifactId>mysql-connector-java</artifactId>   <version>8.0.13</version>   <scope>runtime</scope> </dependency>

 

第三步:创建JDBC.xml配置文件并设置

? 1 2 3 4 5 6 7 8 <?xml version='1.0' encoding='UTF-8'?> <accounts>   <account>     <url>jdbc:mysql://localhost:3306/mybase?useSSL=false&amp;serverTimezone=CTT</url>     <user>root</user>     <password>123456</password>   </account> </accounts>

在src下创建JDBC.xml,这个xml文件中放置的是数据库连接时要使用的信息,包括url、root、password。因为我使用的是MySQL8.0,所以url和之前版本的有所不同,其中mybase是要连接的数据库的名称,&amp;则是&的转义字符

第四步:创建JDBCUtils和TestJDBCUtils

在com.langsin.jdbcutil包下创建JDBCUtils.java和TestJDBCUtils.java两个文件

第五步:写入JDBCUtils和TestJDBCUtils

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 package com.langsin.jdbcutil;   import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.sql.*;   public class JDBCUtils {  private JDBCUtils {}  private static Connection con;    static {   try {    //初始化MySQL的Driver类    Class.forName("com.mysql.cj.jdbc.Driver");    //通过dom4j得到xml文件中连接数据库的信息    SAXReader reader = new SAXReader();    Document doc = reader.read("src/JDBC.xml");    Element root = doc.getRootElement();    Element ele = root.element("account");    String url = ele.element("url");    String user = ele.element("user");    String password = ele.element("password");    //连接数据库    con = DriverManager.getConnection(url, user, password);   } catch(Exception e) {    throw new RuntimeException(e + ",数据库连接失败!");   }  }    public static Connection getConnection() {   return con;  }    public static void close(Connection con, Statement state) {   if(con != null) {    try {     con.close();    } catch (SQLException e) {     e.printStackTrace();    }   }   if(state != null) {    try {     state.close();    } catch (SQLException e) {     e.printStackTrace();    }   }  }    public static void close(Connection con, Statement state, ResultSet rs) {   if(con != null) {    try {     con.close();    } catch (SQLException e) {     e.printStackTrace();    }   }   if(state != null) {    try {     state.close();    } catch (SQLException e) {     e.printStackTrace();    }      if(rs != null) {    try {     rs.close();    } catch (SQLException e) {     e.printStackTrace();    }   }  } } package com.langsin.jdbcutil;   import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet;   public class TestJDBCUtils {  public static void main(String[] args) {   Connection con = JDBCUtils.getConnection();   String sql = "SELECT * FROM sort";   //创建PreparedStatement对象,并将sql语句发送到数据库   PreparedStatement pst = con.prepareStatement(sql);   //取得执行后的结果集   ResultSet rs = pst.executeQuery();   //输出sort表中第二列的所有数据   while(rs.next()) {    System.out.println(rs.getString(2));   }   JDBCUtils.close(con, pst, rs);  } }

好了,到此只要执行程序,控制台上就会输出我们想要的结果了。

总结

以上所述是小编给大家介绍的在IDEA的maven项目中连接并使用MySQL8.0的方法教程,希望对大家有所帮助!

原文链接:https://www.cnblogs.com/ypf666/archive/2020/02/05/12263511.html