摘要:做到每個敲下的按鍵每次鼠標(biāo)的點擊每次手機屏幕的觸碰,都能產(chǎn)生價值開發(fā)環(huán)境為了開發(fā)應(yīng)用級的程序,整個開發(fā)鏈比較繁瑣復(fù)雜,所以會需要更多的時間來掌握。注有關(guān)詳細(xì)信息請使用重新編譯。執(zhí)行文件執(zhí)行結(jié)果,打印表信息如下至此,與成功實現(xiàn)了數(shù)據(jù)交互。
ArthurSlog
SLog-5
Year·1
Guangzhou·China
July 11th 2018
做到每個敲下的按鍵、每次鼠標(biāo)的點擊、每次手機屏幕的觸碰,都能產(chǎn)生價值
開發(fā)環(huán)境MacOS(High Sierra 10.13.5)Java為了開發(fā)應(yīng)用級的程序,整個開發(fā)鏈比較繁瑣復(fù)雜,所以會需要更多的時間來掌握。
老規(guī)則,讓我們明確一下需要的信息Mysql官方手冊
打開命令行,測試一下java,如果提示沒有安裝JDK,移步JavaJDK下載
安裝完JDK,測試一下安裝是否完整
java -version
java version "10.0.1" 2018-04-17 Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10) Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
javac -version
javac 10.0.1
在macOS中,JDK安裝路徑是/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home
在macOS中,JRE安裝路徑是/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
下載Connector / J,選擇platform Independent,選擇Platform Independent (Architecture Independent), ZIP Archive下載
下載好Connector / J后解壓文件得到文件夾,配置 CLASSPATH
export CLASSPATH=~/Downloads/mysql-connector-java-8.0.11.jar:$CLASSPATH
下面是完整代碼
LoadDriver.java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; // assume that conn is an already created JDBC connection (see previous examples) public class LoadDriver { public static void main(String[] args) { try { // The newInstance() call is a work around for some // broken Java implementations Class.forName("com.mysql.cj.jdbc.Driver").newInstance(); Connection conn = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost/my_db?" + "user=root&password=88888888"); // Do something with the Connection Statement stmt = null; ResultSet rs = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM Account"); // or alternatively, if you don"t know ahead of time that // the query will be a SELECT... if (stmt.execute("SELECT * FROM Account")) { rs = stmt.getResultSet(); } // Now do something with the ResultSet .... ResultSetMetaData rsmd = rs.getMetaData(); int columnsNumber = rsmd.getColumnCount(); while (rs.next()) { for (int i = 1; i <= columnsNumber; i++) { if (i > 1) System.out.print(", "); String columnValue = rs.getString(i); System.out.print(columnValue + " " + rsmd.getColumnName(i)); } System.out.println(""); } } catch (SQLException ex){ // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } finally { // it is a good idea to release // resources in a finally{} block // in reverse-order of their creation // if they are no-longer needed if (rs != null) { try { rs.close(); } catch (SQLException sqlEx) { } // ignore rs = null; } if (stmt != null) { try { stmt.close(); } catch (SQLException sqlEx) { } // ignore stmt = null; } } } catch (SQLException ex) { // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } } catch (Exception ex) { // handle the error System.out.println("Exception: " + ex.getMessage()); } } }執(zhí)行流程如下:
用java應(yīng)用程序注冊你的mysql jdbc驅(qū)動程序,參考Mysql/JDBC執(zhí)行SQL手冊
Class.forName("com.mysql.cj.jdbc.Driver").newInstance()
連接數(shù)據(jù)庫
conn =DriverManager.getConnection("jdbc:mysql://localhost/my_db?" + "user=root&password=88888888")
獲得連接對象,并執(zhí)行SQL指令
Statement stmt = null; ResultSet rs = null; stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM Account");
獲得SQL執(zhí)行結(jié)果,對結(jié)果進行打印
ResultSetMetaData rsmd = rs.getMetaData(); int columnsNumber = rsmd.getColumnCount(); while (rs.next()) { for (int i = 1; i <= columnsNumber; i++) { if (i > 1) System.out.print(", "); String columnValue = rs.getString(i); System.out.print(columnValue + " " + rsmd.getColumnName(i)); } System.out.println(""); }
打印SQL結(jié)果
ResultSet resultSet = statement.executeQuery("SELECT * from foo"); ResultSetMetaData rsmd = resultSet.getMetaData(); int columnsNumber = rsmd.getColumnCount(); while (resultSet.next()) { for (int i = 1; i <= columnsNumber; i++) { if (i > 1) System.out.print(", "); String columnValue = resultSet.getString(i); System.out.print(columnValue + " " + rsmd.getColumnName(i)); } System.out.println(""); }
切換到LoadDriver.java文件路徑下,編譯LoadDriver.java文件
javac LoadDriver.java
編譯生成LoadDriver.class,編譯提示
注: LoadDriver.java使用或覆蓋了已過時的 API。 注: 有關(guān)詳細(xì)信息, 請使用 -Xlint:deprecation 重新編譯。
執(zhí)行LoadDriver.class文件
java LoadDriver
執(zhí)行結(jié)果,打印Account表信息如下:
1 ID, ArthurSlog AccountName, ArthurSlog Password
至此,Java與MySQL成功實現(xiàn)了數(shù)據(jù)交互。
歡迎關(guān)注我的微信公眾號 ArthurSlog 如果你喜歡我的文章 歡迎點贊 留言 謝謝文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/76817.html
摘要:現(xiàn)在,對接文件已經(jīng)到位接下來測試數(shù)據(jù)庫,如果還沒安裝或者安裝了還沒配置,移步數(shù)據(jù)庫配置,完成數(shù)據(jù)庫的安裝和配置或者在官方下載,安裝官方手冊安裝。 ArthurSlog SLog-3 Year·1 Guangzhou·China July 9th 2018 showImg(https://segmentfault.com/img/remote/1460000016093266?w=2...
閱讀 796·2021-09-22 16:01
閱讀 2093·2021-08-20 09:37
閱讀 1696·2019-08-30 15:54
閱讀 1697·2019-08-30 15:44
閱讀 835·2019-08-28 18:23
閱讀 3017·2019-08-26 12:17
閱讀 1021·2019-08-26 11:56
閱讀 1544·2019-08-23 16:20