教你用eclipse連接mysql數(shù)據(jù)庫
由于總是出錯(cuò),記錄一下連接MySQL數(shù)據(jù)庫的過程。
連接過程
1.下載MySQL并安裝,這里的版本是8.0.18
2.下載MySQL的jdbc,下載后解壓,個(gè)人將其保存在MySQL目錄下,方便查找
3.連接數(shù)據(jù)庫
(1)eclipse中選擇Window-preferences-java-Build Path-User Libraries
(2)點(diǎn)擊右側(cè)的new按鈕,
(3)在這里輸入jdbc,選中對(duì)勾,點(diǎn)擊ok
(4)回到上一級(jí)界面,點(diǎn)擊Add External JARs,打開到你的jdbc存放的目錄,打開-ok。
(5)接下來是在項(xiàng)目中導(dǎo)入jar包,項(xiàng)目右鍵-Build Path-Configure Build Path
(6)點(diǎn)擊右側(cè)Add Library… -User Library-Next。打上對(duì)勾點(diǎn)擊finish
(7)回到上一級(jí)界面就可以看到你添加的jdbc,點(diǎn)擊Apply再點(diǎn)擊ok。
(8)這樣在你的項(xiàng)目下就可以看到你導(dǎo)入的jdbc了
4.在項(xiàng)目中Java resources下創(chuàng)建一個(gè)新的包linkMysql,里面新建一個(gè)類Demo
代碼如下:
package linkMysql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Demo { // 加載數(shù)據(jù)庫驅(qū)動(dòng) com.mysql.jdbc.Driver private static String dbdriver = "com.mysql.cj.jdbc.Driver"; //因?yàn)镸ySQL是8.0版本,所以需要加上cj,如果是5.0版本就不用 // 獲取mysql連接地址 private static String dburl = "jdbc:mysql://127.0.0.1:3306/cmxDatabaseName?&useSSL=false&serverTimezone=UTC"; //這里的&serverTimezone=UTC很重要,之前就是因?yàn)檫@個(gè)出錯(cuò) // 數(shù)據(jù)名稱 private static String username = "root"; // 數(shù)據(jù)庫密碼 private static String userpassword = "123456"; // 獲取一個(gè)數(shù)據(jù)的連接 public static Connection conn = null; // 獲取連接的一個(gè)狀態(tài) //下面是一個(gè)例子,其中database1是數(shù)據(jù)庫名,后面是一條查詢語句 public static void main(String[] args) throws SQLException { List<List<Object>> x = getData("database1", "select * from students"); System.out.println(x); } /** * 獲取數(shù)據(jù)庫連接 * * @param myProjName * @return */ private static Connection getConn(String myProjName) { Connection conn = null; try { Class.forName(dbdriver); String myjdbcUrl = dburl.replace("cmxDatabaseName", myProjName); conn = DriverManager.getConnection(myjdbcUrl, username, userpassword); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } /** * 關(guān)閉數(shù)據(jù)庫連接 * * @param rs * @param ps * @param conn */ private static void closeAll(ResultSet rs, PreparedStatement ps, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn == null) return; try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } /** * 查表,返回行的列表,每個(gè)列表中包含列的列表。 * * @param ProjName * @param sql * @return */ public static List<List<Object>> getData(String ProjName, String sql) { Connection conn = getConn(ProjName); PreparedStatement ps = null; List<List<Object>> list = new ArrayList<List<Object>>(); ResultSet rs = null; try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); ResultSetMetaData md = rs.getMetaData(); int columnCount = md.getColumnCount(); while (rs.next()) { List<Object> lst = new ArrayList<Object>(); for (int i = 1; i <= columnCount; ++i) { lst.add(rs.getObject(i) == null ? "" : rs.getObject(i)); } list.add(lst); } } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(rs, ps, conn); } return list; } }
5.將該類以Java application運(yùn)行,就可以在控制臺(tái)看見students表中的全部信息
到此這篇關(guān)于教你用eclipse連接mysql數(shù)據(jù)庫的文章就介紹到這了,更多相關(guān)eclipse連接mysql數(shù)據(jù)庫內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。