java数据库连接(JDBC)
步骤:
- 在菜单栏新建web project。
- 在项目中新建servlet。
- 将连接数据库代码在src目录下。
- 将mysql-connector-java-5.1.47-bin.jar解压,放到项目的:项目名\WebRoot\WEB-INF\lib目录下。
- 右键项目,选择Build Path中的configure build path,在library中选择Add External Jars..,选择mysql-connector-java-5.1.47-bin.jar压缩包,点击apply。
- 以Java Application 运行程序。
A simple jdbc project (ga6v)
常见错误:
Sat Apr 06 13:53:28 CST 2019 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.
Exception in thread "main" java.sql.SQLException: Access denied for user 'scott'@'localhost' (using password: YES)
问题:数据库用户名或密码不正确
解决方法:查看下面代码中的用户名与密码填写是否正确(加粗部分)
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/javabook", "scott", "tiger");
Exception in thread "main" java.sql.SQLException: Client does not support authentication protocol requested by server; consider upgrading MySQL client
问题:使用的mysql-connector-java-5.1.47-bin.jar版本和数据库版本不匹配,可能mysql-connector-java的版本太老
解决方法:下载最新的mysql-connector-java-5.1.47-bin.jar版本
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
问题:没有在library中添加jar包
解决方法:参看“步骤”中的3-5步