前言
由于總是出錯,記錄一下連接MySQL數(shù)據(jù)庫的過程。
連接過程
1.下載MySQL并安裝,這里的版本是8.0.18
2.下載MySQL的jdbc,下載后解壓,個人將其保存在MySQL目錄下,方便查找



3.連接數(shù)據(jù)庫
(1)eclipse中選擇Window-preferences-java-Build Path-User Libraries

(2)點(diǎn)擊右側(cè)的new按鈕,

(3)在這里輸入jdbc,選中對勾,點(diǎn)擊ok

(4)回到上一級界面,點(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。打上對勾點(diǎn)擊finish

(7)回到上一級界面就可以看到你添加的jdbc,點(diǎn)擊Apply再點(diǎn)擊ok。

(8)這樣在你的項(xiàng)目下就可以看到你導(dǎo)入的jdbc了

4.在項(xiàng)目中Java resources下創(chuàng)建一個新的包linkMysql,里面新建一個類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ū)動 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=falseserverTimezone=UTC";
//這里的serverTimezone=UTC很重要,之前就是因?yàn)檫@個出錯
// 數(shù)據(jù)名稱
private static String username = "root";
// 數(shù)據(jù)庫密碼
private static String userpassword = "123456";
// 獲取一個數(shù)據(jù)的連接
public static Connection conn = null;
// 獲取連接的一個狀態(tài)
//下面是一個例子,其中database1是數(shù)據(jù)庫名,后面是一條查詢語句
public static void main(String[] args) throws SQLException {
ListListObject>> 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();
}
}
/**
* 查表,返回行的列表,每個列表中包含列的列表。
*
* @param ProjName
* @param sql
* @return
*/
public static ListListObject>> getData(String ProjName, String sql) {
Connection conn = getConn(ProjName);
PreparedStatement ps = null;
ListListObject>> list = new ArrayListListObject>>();
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
while (rs.next()) {
ListObject> lst = new ArrayListObject>();
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)行,就可以在控制臺看見students表中的全部信息

到此這篇關(guān)于教你用eclipse連接mysql數(shù)據(jù)庫的文章就介紹到這了,更多相關(guān)eclipse連接mysql數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Eclipse中C++連接mysql數(shù)據(jù)庫
- myeclipse中連接mysql數(shù)據(jù)庫示例代碼
- Myeclipse連接mysql數(shù)據(jù)庫心得體會
- MyEclipse連接MySQL數(shù)據(jù)庫圖文教程
- Eclipse連接Mysql數(shù)據(jù)庫操作總結(jié)
- MyEclipse連接Mysql數(shù)據(jù)庫的方法(一)
- 傻瓜式用Eclipse連接MySQL數(shù)據(jù)庫
- 用Eclipse連接MySQL數(shù)據(jù)庫的步驟
- MyEclipse連接MySQL數(shù)據(jù)庫報(bào)錯解決辦法
- MyEclipse通過JDBC連接MySQL數(shù)據(jù)庫基本介紹