利用eclipse連接數(shù)據(jù)庫(kù)
第一步:先下載mysql
網(wǎng)址是:https://dev.mysql.com/downloads/installer/
?
第二步:下載完成后就可以安裝了
1.雙擊下載的mysql-installer-web-community-5.7.17.0.msi文件
進(jìn)行安裝。
2.安裝過(guò)后打開(kāi)cmd輸入net srart mysql57
?
第三步:下載jdbc
1.這是下載jdbc的網(wǎng)址:https://www.mysql.com/products/connector/
?
2.
?
第四步:下載成功后這里就要使用eclipse連接數(shù)據(jù)庫(kù)了
1.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,打開(kāi)到你的jdbc存放的目錄,打開(kāi)-ok。
?
5.接下來(lái)是項(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了
?
第五步就是利用cmd打開(kāi)mysql創(chuàng)建一個(gè)表
1.打開(kāi)cmd輸入mysql -uroot -p123456
?
2.然后再輸入show databases;
這里記著一定要加分號(hào);英文狀態(tài)下的分號(hào)。
?
3.創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)
create database user_cmx;
創(chuàng)建一個(gè)表
use user_cmx;
為數(shù)據(jù)庫(kù)表添加字段
create table user(name varchar(20),age varchar(10));
?
第六步:創(chuàng)建完成后,我們就可以使用eclipse連接數(shù)據(jù)庫(kù)了。
最后一步就是我們的代碼了
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ù)庫(kù)驅(qū)動(dòng) com.mysql.jdbc.Driver
private static String dbdriver = “com.mysql.jdbc.Driver”;
// 獲取mysql連接地址
private static String dburl = “jdbc:mysql://127.0.0.1:3306/cmxDatabaseName?&useSSL=false”;
// 數(shù)據(jù)名稱
private static String username = “root”;
// 數(shù)據(jù)庫(kù)密碼
private static String userpassword = “123456”;
// 獲取一個(gè)數(shù)據(jù)的連接
public static Connection conn = null;
// 獲取連接的一個(gè)狀態(tài)
public static void main(String[] args) throws SQLException {
List《List《Object》》 x = getData(“user_cmx”,
“select name,age from user”);
System.out.println(“x=” + x);
}
/**
* 獲取數(shù)據(jù)庫(kù)連接
*
* @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ù)庫(kù)連接
*
* @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;
}
}
這里的dbdriver和dburl一定要寫(xiě)正確。如果驅(qū)動(dòng)名稱dbdriver寫(xiě)錯(cuò)會(huì)報(bào)一個(gè)異常,加入我少寫(xiě)了一個(gè)單詞字母,異常如下
?
如果我的dburl出錯(cuò)了,會(huì)拋出以下異常
?
如果是dburl的最后“?&useSSL=false”沒(méi)有寫(xiě)的話會(huì)報(bào)出一個(gè)警告,如下:
?
警告全部?jī)?nèi)容:
Sun Feb 05 17:15:15 CST 2017 WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’。 You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
好了,到這里我們的數(shù)據(jù)庫(kù)就連接成功了。
評(píng)論