In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
这篇文章主要介绍了在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包坐标
org.dom4j dom4j 2.1.1
2.mysql的jar包坐标
mysql mysql-connector-java 8.0.13 runtime
第三步:创建JDBC.xml配置文件并设置
jdbc:mysql://localhost:3306/mybase?useSSL=false&serverTimezone=CTT root 123456
在src下创建JDBC.xml,这个xml文件中放置的是数据库连接时要使用的信息,包括url、root、password。因为我使用的是MySQL8.0,所以url和之前版本的有所不同,其中mybase是要连接的数据库的名称,&则是&的转义字符
第四步:创建JDBCUtils和TestJDBCUtils
在com.langsin.jdbcutil包下创建JDBCUtils.java和TestJDBCUtils.java两个文件
第五步:写入JDBCUtils和TestJDBCUtils
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"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.