在一個(gè)項(xiàng)目當(dāng)中,數(shù)據(jù)庫(kù)是非常的重要,用來存儲(chǔ)各種數(shù)據(jù),來供給編程語言(Java)等來進(jìn)行交互。本文將會(huì)為您介紹如何從網(wǎng)上下載MySQL數(shù)據(jù)庫(kù),到在IDEA軟件上Java連接數(shù)據(jù)庫(kù)的詳細(xì)步驟。
一.導(dǎo)入jar包
1.下載jar包:https://dev.mysql.com/downloads/
2.導(dǎo)入
在項(xiàng)目文件夾下新建一個(gè)名為lib的文件夾
將下載好的jar包放入lib文件夾,然后右擊lib文件夾,選擇Add as Library...,然后點(diǎn)擊ok
二.代碼部分
1.加載驅(qū)動(dòng)
Class.forName("com.mysql.cj.jdbc.Driver");
2.用戶信息和url
String url = "jdbc:mysql://localhost:3306/數(shù)據(jù)庫(kù)名?&useSSL=false&serverTimezone=UTC";
3.數(shù)據(jù)庫(kù)對(duì)象Connection
Connection connection = DriverManager.getConnection(url,username,password);
4.執(zhí)行數(shù)據(jù)庫(kù)對(duì)象connection
Statement statement = connection.createStatement();
代碼展示
package com.lofun.fuxi.JDBC;
import java.sql.*;
public class jdbc_used {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加載驅(qū)動(dòng)
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println( "加載驅(qū)動(dòng)成功!" );
//2.用戶信息和url
String url = "jdbc:mysql://localhost:3306/練習(xí)?&useSSL=false&serverTimezone=UTC";
String username = "root"; //數(shù)據(jù)庫(kù)用戶名
String password = "123456";//數(shù)據(jù)庫(kù)密碼
//3.連接成功,數(shù)據(jù)庫(kù)對(duì)象Connection代表數(shù)據(jù)庫(kù)
Connection connection = DriverManager.getConnection(url,username,password);
//4.執(zhí)行SQL的對(duì)象Statement
Statement statement = connection.createStatement();
String sql = "SELECT * FROM students";
ResultSet resultSet_01 = statement.executeQuery(sql);//查詢的結(jié)果集,封裝了所有的查詢結(jié)果 statement.executeQuery()執(zhí)行sql語句
while(resultSet_01.next()){
System.out.println(resultSet_01 .getObject("name"));//resultSet_01 .getObject獲取指定的數(shù)據(jù)類型
}
//關(guān)閉
resultSet_01.close();
statement.close();
connection.close();
}
}
以上就是下載MySQL數(shù)據(jù)庫(kù)和在IDEA軟件上完成Java連接MySQL8.0 JDBC詳細(xì)步驟的內(nèi)容,更多相關(guān)Java內(nèi)容請(qǐng)搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章!