发表于: 2020-02-09 23:48:36
1 1305
今天完成的事情:
* 了解jdbc的常用类和使用JDBC连接数据库
* 了解jdbcTemplate
JDBC(Java DataBase Connectivity),是一种用于执行SQL语句的Java API。
JDBC操作数据库步骤:
- 加载驱动程序
- 建立连接
- 向数据库发送SQL语句
- 处理返回结果
- 关闭连接
常用的类与接口
DriveManager
管理数据库的驱动程序。
连接mysql5版本的驱动:
Class.forName("com.myslq.jdbc.Driver")
Connection
指定java与数据库的连接
通过访问数据库的url获取数据库连接对象
DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jnshu","root","123456");
Statement
执行静态SQL语句的工具接口
PreparedStatement
执行动态SQL语句的工具接口
ResultSet
暂时存放查询后的结果
连接数据库
import java.sql.*;public class Conn { Connection con; public Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("数据库驱动加载成功"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { con = DriverManager.getConnection("jdbc:mysql:" +"//127.0.0.1:3306/jnshu","root","123456"); System.out.println("DB connect successfully"); } catch (SQLException e) { e.printStackTrace(); } return con; } public static void main(String[] args) { Conn c = new Conn(); c.getConnection(); }}
明天计划的事情:
* 了解jdbcTempalte连接和spring
* 了解Mybatis连接
遇到的问题:因为用的os.15,一开始装的mysql8
1)驱动版本问题:
java.lang.UnsupportedClassVersionError: com/mysql/jdbc/Driver : Unsupported major.minor version 52.0
java7,不支持6以上驱动的版本,使用5.1.24
2)mysql8的加密规则变了
java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.
数据库版本降级
收获:
版本兼容太头疼,使用一个技术注意他对上对下的版本。还好os15装了mysql5.7也可以用。不然要不升级jdk,要不系统降级。。。
评论