Java學習經驗集合Ⅱ(1-5條)

1.圖片使用問題:圖片資源文件夾放置的位置以及使用的方法

起因代碼片段:

JPanel splash = new JPanel(new BorderLayout());
  URL url = getClass().getResource("5.jpg");
  System.out.println(url);
  if(url != null) {
   splash.add(new JLabel(new ImageIcon(url)),BorderLayout.CENTER);
  }
     setContentPane(splash);

錯誤原因:起先,我創建了image文件夾在工程文件夾的src文件夾下,並把圖片5.jpg放到image文件夾,發現程序運行後沒有顯示;

解決辦法:把放置圖片的image文件夾放到與src文件夾平行的位置即可。

注意:工程文件夾創建的時候不要帶中文,否則獲取的url中與中文相關的部分爲亂碼,圖片也不能顯示。

2.如何將Java程序export成jar包後用bat文件執行:

啓動一:start jre\bin\javaw.exe -cp .\bin\ -Djava.ext.dirs=. d.SwingUI         (注:SwingUI是主方法名稱

啓動二:java -jar 生詞本.jar

流程:先在jar文件所在目錄下創建一個txt文檔,協商相應的語句後保存修改後綴名爲bat

3.起因:手動添加MySQL與java連接的驅動後出現以下提示

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

在使用5.1.44和8.0.11兩個版本的驅動進行嘗試後發現只有高版本的驅動出現提示

解決方案:低版本的驅動使用Class.forName("com.mysql.jdbc.Driver");

高版本的驅動使用Class.forName("com.mysql.cj.jdbc.Driver");

4.起因:

4.1報錯SQLException: The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

查詢資料發現這是由於數據庫和系統時區差異所造成的,在jdbc連接的url後面加上serverTimezone=GMT即可解決問題

4.2報錯Thu May 24 22:20:36 CST 2018 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.

翻譯:THU 5月24日22:20:36 CST 2018警告:不建議建立沒有服務器身份驗證的SSL連接。根據MySQL 5.5.45 +、5.626+和5.7.6+的要求,如果沒有設置顯式選項,默認情況下必須建立SSL連接。對於不使用SSL的現有應用程序,ValuyServer證書屬性設置爲“false”。您需要通過設置USESL= false來顯式禁用SSL,或者設置USELS=真,併爲服務器證書驗證提供信任存儲。

解決辦法:建立連接時,顯式聲明不驗證SSL,在連接mysql的url上加上useSSL=false

例句:String url = "jdbc:mysql://localhost:3306/studentinfo?useSSL=false&serverTimezone=GMT";

注意:數據庫名稱和加的參數用  ? 分隔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章