executequery方法_在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)
org.dom4j dom4j 2.1.12.mysql的jar包坐標(biāo)
mysql mysql-connector-java 8.0.13runtime第三步:創(chuàng)建JDBC.xml配置文件并設(shè)置
<?xml version='1.0' encoding='UTF-8'?> jdbc:mysql://localhost:3306/mybase?useSSL=false&serverTimezone=CTTroot123456在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的方法教程,希望對大家有所幫助!
總結(jié)
以上是生活随笔為你收集整理的executequery方法_在IDEA的maven项目中连接使用MySQL8.0方法教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 何小鹏:15 万级的 MONA 车型进展
- 下一篇: mysql数据自动备份_每天自动备份My