idea中Java代码连接MySQL常见问题解决

  1.   没有添加jar程序驱动包导致出现以下问题,即无法找到驱动类。

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

解决方法是可以在    http://www.mysql.com/downloads/connector/j   手动下载Java连接MySQL的最新驱动包,通过intellij idea导入即可。

点击 File ->  Project Structure(快捷键 Ctrl + Alt + Shift + s),点击Project Structure界面左侧的“Modules”;

在 “Dependencies” 标签界面下,点击右边的 “+”号,选择第一个选项“JARs or directories...”,选择相应的jar包,点“OK”,jar包添加成功。其他类似错误也可以通过导包解决。     具体可以参考                    http://jingyan.baidu.com/article/fec7a1e5f79e2b1191b4e74f.html  
           static final String DB_URL = "jdbc:mysql://localhost/test";   
           static final String USER = "root";
           static final String PASS = "root123";

程序中对应的数据库名称改为自己的数据库名称,我的是test;用户和密码也改成自己的数据库相对应的,否则会报错。
 
2.   web应用中连接mysql数据库时后台会出现这样的提示:

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.



原因是MySQL在高版本需要指明是否进行SSL连接。解决方案如下:

在mysql连接字符串url中加入ssl=true或者false即可,如下所示。

url="jdbc:mysql://127.0.0.1:3306/framework?characterEncoding=utf8&useSSL=true"
 
3.   图形化界面连接的时候如果测试通不过
 
(1)出现部分的乱码,是因为MySQL版太高
(2)导的jar包不是对应的MySQL版本

猜你喜欢

转载自www.cnblogs.com/shenzhenhuaya/p/15226181250_shenzhenhua03.html
今日推荐