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";

注意:数据库名称和加的参数用  ? 分隔

发布了13 篇原创文章 · 获赞 13 · 访问量 7348

猜你喜欢

转载自blog.csdn.net/lvdoujack/article/details/80427113