-
注意:本地安装Myql务必使用5.7版本
-
原生jdbc访问数据库步骤
- 加载JDBC驱动程序
- 创建数据库的连接
- 创建preparedStatement
- 执行SQL语句
- 处理结果集
- 关闭JDBC对象资源
-
Springboot项目测试原生JDBC连接
-
加载依赖包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
Main函数访问代码实操
public static void main(String [] args) throws Exception { Class.forName("com.mysql.cj.jdbc.Driver"); //连接 String url = "jdbc:mysql://127.0.0.1:3306/xdclass?useUnicode=true&characterEncoding=utf-8&useSSL=false"; String username = "root"; String password = "xdclass.net"; //获取连接对象,并连接数据库 Connection connection = DriverManager.getConnection(url,username,password); //获取语句对象 Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("select * from video"); while (resultSet.next()){ System.out.println("视频标题:"+resultSet.getString("title")); } statement.close(); }
javaweb通过原生jdbc访问数据库
最新推荐文章于 2023-02-13 11:57:40 发布