欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

eclipse怎么配置mysql數據庫

錢衛國1年前12瀏覽0評論

Eclipse作為一個集成開發環境可以方便地對數據庫進行操作,配置MySQL數據庫只需要簡單的幾步操作即可。下面就讓我們一起來看看如何配置MySQL數據庫。

第一步: 在Eclipse中安裝插件

菜單欄 — Help — Eclipse MarketPlace
搜索MySQL,安裝MySQL connector/J 5.1.44版本插件

第二步:導入MySQL的jar包

選擇項目 — build path — configure build path — java build path — libraries — Add JARs...
選擇MySQL Connector/J的jar包(/mysql-connector-java-5.1.44-bin.jar)

第三步:編寫代碼

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class MySQLDemo {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/db_name?useUnicode=true&characterEncoding=utf8";
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSetMetaData rsmd = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
pstmt = conn.prepareStatement("select * from table_name");
rsmd = pstmt.getMetaData();
int columnCount = rsmd.getColumnCount();
for (int i = 1; i<= columnCount; i++) {
System.out.print(rsmd.getColumnLabel(i) + "\t");
}
System.out.println();
while (pstmt.getResultSet().next()) {
for (int i = 1; i<= columnCount; i++) {
System.out.print(pstmt.getResultSet().getString(i) + "\t");
}
System.out.println();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

第四步:運行程序

在Eclipse的項目上右鍵 — Run As — Java Application,即可執行程序。