在IDEA的maven項目中連接并使用MySQL8.0的方法教程
首先看一下我的基本的開發(fā)環(huán)境:
操作系統(tǒng):MacOS 10.13.5編輯器:IDEA 2018.3其他:MySQL8.0.15、Maven 3.3.9、JDK 1.8
好,下面就正式開始:
第一步:在IDEA中新建一個maven項目
1.使用骨架創(chuàng)建maven項目,此處選擇:maven-archetype-quickstart
2.填入GroupId和ArtifactId
3.第一個選中maven安裝的文件夾,第二個選中maven安裝文件夾中的conf/settings.xml,第三個如果settings.xml中配置了localRepository,則會自動填入,若沒有則會顯示默認(rèn)的本地倉庫
4.點擊Finish即可成功創(chuàng)建maven項目
第二步:配置pom.xml
在pom.xml中的標(biāo)簽內(nèi)加入要用到的jar包在倉庫中的坐標(biāo)
1.dom4j的jar包坐標(biāo)
<dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <version>2.1.1</version> </dependency>
2.mysql的jar包坐標(biāo)
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> <scope>runtime</scope> </dependency>
第三步:創(chuàng)建JDBC.xml配置文件并設(shè)置
<?xml version='1.0' encoding='UTF-8'?> <accounts> <account> <url>jdbc:mysql://localhost:3306/mybase?useSSL=false&serverTimezone=CTT</url> <user>root</user> <password>123456</password> </account> </accounts>
在src下創(chuàng)建JDBC.xml,這個xml文件中放置的是數(shù)據(jù)庫連接時要使用的信息,包括url、root、password。因為我使用的是MySQL8.0,所以url和之前版本的有所不同,其中mybase是要連接的數(shù)據(jù)庫的名稱,&則是&的轉(zhuǎn)義字符
第四步:創(chuàng)建JDBCUtils和TestJDBCUtils
在com.langsin.jdbcutil包下創(chuàng)建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文件中連接數(shù)據(jù)庫的信息 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"); //連接數(shù)據(jù)庫 con = DriverManager.getConnection(url, user, password); } catch(Exception e) { throw new RuntimeException(e + ",數(shù)據(jù)庫連接失?。?); } } 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"; //創(chuàng)建PreparedStatement對象,并將sql語句發(fā)送到數(shù)據(jù)庫 PreparedStatement pst = con.prepareStatement(sql); //取得執(zhí)行后的結(jié)果集 ResultSet rs = pst.executeQuery(); //輸出sort表中第二列的所有數(shù)據(jù) while(rs.next()) { System.out.println(rs.getString(2)); } JDBCUtils.close(con, pst, rs); } }
好了,到此只要執(zhí)行程序,控制臺上就會輸出我們想要的結(jié)果了。
總結(jié)
以上所述是小編給大家介紹的在IDEA的maven項目中連接并使用MySQL8.0的方法教程,希望對大家有所幫助!
相關(guān)文章
一文帶你將csv文件導(dǎo)入到mysql數(shù)據(jù)庫(親測有效)
一直不大懂csv怎么通過mysql圖形化的界面直接導(dǎo)入,看了很多帖,才覺得自己會了,下面這篇文章主要給大家介紹了關(guān)于將csv文件導(dǎo)入到mysql數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下2022-08-08SQL update多表關(guān)聯(lián)更新方法解讀
這篇文章主要介紹了SQL update 多表關(guān)聯(lián)更新方法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08MySQL拋出Incorrect string value異常分析
從上至下統(tǒng)一用上UTF-8就高枕無憂,今天還是遇到字符的異常,本文將介紹解決方法2012-11-11