首先看一下我的基本的開發(fā)環(huán)境:
操作系統(tǒng):MacOS 10.13.5編輯器:IDEA 2018.3其他:MySQL8.0.15、Maven 3.3.9、JDK 1.8
好,下面就正式開始:
第一步:在IDEA中新建一個(gè)maven項(xiàng)目
1.使用骨架創(chuàng)建maven項(xiàng)目,此處選擇:maven-archetype-quickstart

2.填入GroupId和ArtifactId

3.第一個(gè)選中maven安裝的文件夾,第二個(gè)選中maven安裝文件夾中的conf/settings.xml,第三個(gè)如果settings.xml中配置了localRepository,則會自動填入,若沒有則會顯示默認(rèn)的本地倉庫

4.點(diǎn)擊Finish即可成功創(chuàng)建maven項(xiàng)目

第二步:配置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,這個(gè)xml文件中放置的是數(shù)據(jù)庫連接時(shí)要使用的信息,包括url、root、password。因?yàn)槲沂褂玫氖荕ySQL8.0,所以url和之前版本的有所不同,其中mybase是要連接的數(shù)據(jù)庫的名稱,&則是&的轉(zhuǎn)義字符
第四步:創(chuàng)建JDBCUtils和TestJDBCUtils
在com.langsin.jdbcutil包下創(chuàng)建JDBCUtils.java和TestJDBCUtils.java兩個(gè)文件

第五步:寫入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); }}            
新聞熱點(diǎn)
疑難解答
圖片精選