JDBC详解系列(一)之流程
来源:互联网 发布:109普陀秒的数据 编辑:程序博客网 时间:2024/05/16 02:22
JDBC概述
使用JDBC也挺长时间了,最近因为想学习mybatis的源码,因此打算重新复习一下JDBC的使用。
定义:JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序。
也就是说JDBC是SUN公司提出来的一系列规范,但它只定义了接口规范,具体的实现则交给各个数据库的厂商去做。这类似以前的软件要调用打印机的时候都要自己去给各种类型的打印机实现驱动,但微软在操作系统定义了打印机驱动接口,由各个打印机厂商去实现,而软件供应商只需要调用接口即可(不然每个数据库都要自己去实现驱动,会累死的)。我想这也符合JAVA跨平台的思想,实现“Write once, run anywhere!”。
JDBC使用详解
先上代码,一个简单的连接,查询用户信息:
/** * 如果你要使用我的代码,在此之前请在mysql创建jdbc_test数据库,并建立student表,两个字段,name和age */public class JDBCTest { /** * 数据库相关参数 */ //这是驱动名称,此例子中我们加载的是mysql的驱动,在之前需要导入mysql的驱动jar包 public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; //连接数据库的url,各个数据库厂商不一样,此处为mysql的;后面是创建的数据库名称 public static final String JDBC_URL = "jdbc:mysql://localhost:3306/jdbc_test"; //连接数据库所需账户名 public static final String JDBC_USERNAME = "root"; //用户名对应的密码,我的mysql密码是123456 public static final String JDBC_PASSWORD ="123456"; public static void main(String[] args) { List<Student> students = new ArrayList<Student>(); Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { //第一步:加载Driver类,注册数据库驱动 Class.forName(JDBC_DRIVER); //第二步:通过DriverManager,使用url,用户名和密码建立连接(Connection) connection = DriverManager.getConnection(JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD); //第三步:通过Connection,使用sql语句打开Statement对象; preparedStatement = connection.prepareStatement("select * from student where age =?"); //传入参数,之所以这样是为了防止sql注入 preparedStatement.setInt(1, 18); //第四步:执行语句,将结果返回resultSet resultSet = preparedStatement.executeQuery(); //第五步:对结果进行处理 while (resultSet.next()){ String name = resultSet.getString("name"); int age = resultSet.getInt("age"); Student student = new Student(); student.setAge(age); student.setName(name); students.add(student); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { //第六步:倒叙释放资源resultSet-》preparedStatement-》connection try { if (resultSet!=null && !resultSet.isClosed()){ resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if(preparedStatement!=null && !preparedStatement.isClosed()){ preparedStatement.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if(connection!=null && connection.isClosed()){ connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } for (Student student:students ) { System.out.println(student.getName()+"="+student.getAge()); } }}
以上就是JDBC查询的基本连接过程,后续一些复杂的数据库操作过程只不过是在上面进行一些增改而已。大体步骤如注释:
JDBC流程:
第一步:加载Driver类,注册数据库驱动;
第二步:通过DriverManager,使用url,用户名和密码建立连接(Connection);
第三步:通过Connection,使用sql语句打开Statement对象;
第四步:执行语句,将结果返回resultSet;
第五步:对结果resultSet进行处理;
第六步:倒叙释放资源resultSet-》preparedStatement-》connection。
如果是删除,修改和插入,使用executeUpdate()即可:
/** * 如果你要使用我的代码,在此之前请在mysql创建jdbc_test数据库,并建立student表,两个字段,name和age */public class JDBCTest { /** * 数据库相关参数 */ //这是驱动名称,此例子中我们加载的是mysql的驱动,在之前需要导入mysql的驱动jar包 public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; //连接数据库的url,各个数据库厂商不一样,此处为mysql的;后面是创建的数据库名称 public static final String JDBC_URL = "jdbc:mysql://localhost:3306/jdbc_test"; //连接数据库所需账户名 public static final String JDBC_USERNAME = "root"; //用户名对应的密码,我的mysql密码是123456 public static final String JDBC_PASSWORD = "123456"; @Test public void testUpdate() { Connection connection = null; PreparedStatement preparedStatement = null; try { //第一步:加载Driver类,注册数据库驱动 Class.forName(JDBC_DRIVER); //第二步:通过DriverManager,使用url,用户名和密码建立连接(Connection) connection = DriverManager.getConnection(JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD); //第三步:通过Connection,使用sql语句打开Statement对象; preparedStatement = connection.prepareStatement("UPDATE student SET age=20 WHERE name=?"); //传入参数,之所以这样是为了防止sql注入 preparedStatement.setString(1, "xiaoming"); //第四步:执行语句,将结果返回resultSet int count = preparedStatement.executeUpdate(); //第五步:对结果进行处理 System.out.println(count); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { //第六步:倒叙释放资源resultSet-》preparedStatement-》connection try { if (preparedStatement != null && !preparedStatement.isClosed()) { preparedStatement.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if (connection != null && connection.isClosed()) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } }}
以及批量操作:
@Test /** * 测试批量操作 */ public void testBatch() { Connection connection = null; PreparedStatement preparedStatement = null; String insertSql = "insert into student values(?,?)"; try { //第一步:加载Driver类,注册数据库驱动 Class.forName(JDBC_DRIVER); //第二步:通过DriverManager,使用url,用户名和密码建立连接(Connection) connection = DriverManager.getConnection(JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD); //第三步:通过Connection,使用sql语句打开Statement对象; preparedStatement = connection.prepareStatement(insertSql); //传入参数,之所以这样是为了防止sql注入 for(int i=0;i<10;i++){ preparedStatement.setString(1,100+i+"user"); preparedStatement.setInt(2,100+i); preparedStatement.addBatch(); } //第四步:执行语句,将结果返回resultSet int[] count = preparedStatement.executeBatch(); //第五步:对结果进行处理 System.out.println(count); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { //第六步:倒叙释放资源resultSet-》preparedStatement-》connection try { if (preparedStatement != null && !preparedStatement.isClosed()) { preparedStatement.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if (connection != null && connection.isClosed()) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } }
今天大概介绍了JDBC连接数据库进行操作的一些具体的流程,后面我会对流程上的各个步骤进行分析,以便在后续学习mybatis源码时有一个更深刻的理解。
- JDBC详解系列(一)之流程
- jdbc源码详解(一):示例+Driver注册流程
- jdbc详解(一)
- 详解JDBC(一)
- JDBC 详解 (一)
- JDBC详解系列(二)之加载驱动
- JDBC系列一:初识JDBC
- JDBC系列教程(一)---连接
- JDBC系列教程(一)---连接
- JDBC系列教程(一)---连接
- JDBC系列教程(一)---连接
- JDBC系列一
- JavaWeb之JDBC(一)
- JavaWeb系列之十二(jdbc)
- jdbc系列之 ACCESS
- 源代码系列之JDBC
- JDBC 详解(干货分享)(一)
- JDBC详解一
- @Pointcut——切点表达式重用
- jdk8中的PermGen永久代去哪里
- 十周第一次课 2017.12.18 安装PHP5、安装PHP7
- Git 的 4 个阶段的撤销更改
- linux kobject kobject_type kset
- JDBC详解系列(一)之流程
- ++ 符号
- C语言中指针的一些概念及用法
- linux下安装mysql数据库
- java 23种设计模式(转)
- 算法的一些小栗子7(贪心算法)
- const int *p与int *const p
- leetcode453_Minimum_moves_to_Equal_array
- 全排列 123 132 213 231 312 321