基础 JDBC 步骤
程序员文章站
2022-11-22 18:48:18
1. 基础 JDBC App == 步骤 0. 下载驱动程序,加入项目的构建路径 MySQL 下载: https://www.mysql.com/products/community/ Driver 下载: https://dev.mysql.com/downloads/file/?id=480091 ......
1. 基础 JDBC App == 步骤
0. 下载驱动程序,加入项目的构建路径
MySQL 下载:
https://www.mysql.com/products/community/
Driver 下载:
https://dev.mysql.com/downloads/file/?id=480091
Driver 文档:
https://dev.mysql.com/doc/connector-j/5.1/en/
1. 导入包
java.sql.*
com.mysql.jdbc.Driver
2. 注册驱动程序
3. 获取连接
url、username、password
4. 声明SQL语句,创建SQL语句的承载的容器 - PreparedStatement
-- DSL Select
-- 设置参数
5. 执行sql语句
-- DSL Select ,
6. 对结果执行操作
-- ResultSet - 遍历
7. 清理环境
8.
下面是创建查询的代码实例
bookshop数据库中对books表的查询
package com.test.jdbc;
import java.sql.*;
public class MyJDBCTest {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
//1.注册驱动程序,整个项目只做一次
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接 url.usn.pwd
String url = "jdbc:mysql://127.0.0.1:3306/bookshop?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
String usn = "root";
String pwd = "1";
Connection conn = DriverManager.getConnection(url, usn, pwd);
//3.声明SQL语句创建sql语句的承载容器 preparedStatement
String sql = "select * from books where price > ?";
//pstmt用于执行查询的SQL语句的容器,同时指定返回结果的仿射
PreparedStatement pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
//pstmt.setDouble(1, 20); 不具有普适性
pstmt.setObject(1, 20);
//4.执行sql语句
ResultSet rs = pstmt.executeQuery();
//5.对返回的结果执行操作
//遍历结果集
if (rs!= null) {
while(rs.next()) {
System.out.println(rs.getInt(1)+"=="+rs.getInt("id"));
}
}
//清理环境.
if (rs != null&&!rs.isClosed()) {
rs.close();
}
if (pstmt != null&&!pstmt.isClosed()) {
pstmt.close();
}
if (conn != null&&!conn.isClosed()) {
conn.close();
}
}
}
还有执行UPDATE语句的实例:
向books表中添加一本书
package com.test.jdbc;
import java.sql.*;
public class MyJDBCTest1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
//1.注册驱动程序,整个项目做一次
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接 url.usn.pwd
String url = "jdbc:mysql://127.0.0.1:3306/bookshop?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
String usn = "root";
String pwd = "1";
Connection conn = DriverManager.getConnection(url, usn, pwd);
//3.声明SQL语句创建sql语句的承载容器 preparedStatement
String sql = "insert into books values(null,?,?,?)";
//pstmt用于执行查询的SQL语句的容器,同时指定返回结果的仿射
PreparedStatement pstmt = conn.prepareStatement(sql);
//设置数据
pstmt.setObject(1, "woaini");
pstmt.setObject(2, 1);
pstmt.setObject(3, 1);
//记录老的事务提交模式,用oldCommitMode这个对象 下面的操作将把MYSQL的默认提交更改成手动提交当然最后还要恢复为默认提交
boolean oldCommitMode = conn.getAutoCommit();
//开启事务,设置事务不是自动提交
conn.setAutoCommit(false);//设置需要手动提交事务
//4.执行sql语句
int result = pstmt.executeUpdate() ;
//完成事务的提交
conn.commit();
//恢复旧的提交模式
conn.setAutoCommit(oldCommitMode);
//5.对返回的结果执行操作
//遍历结果集
System.out.println(result);
//清理环境.
if (pstmt != null&&!pstmt.isClosed()) {
pstmt.close();
}
if (conn != null&&!conn.isClosed()) {
conn.close();
}
}
}
本文地址:https://blog.csdn.net/qq_42658791/article/details/85942423