JDBC的复习

JDBC的概述

  1. JDBC:java database connectivity SUN公司提供的一套操作数据库的标准规范。
    JDBC与数据库驱动的关系:接口与实现的关系。
    在这里插入图片描述
  2. JDBC的规范(四个核心对象):
    DriverManager:用于注册驱动
    Connection: 表示与数据库创建的连接
    Statement: 操作数据库sql语句的对象
    ResultSet: 结果集或一张虚拟表
    在这里插入图片描述

实现JDBC

  1. 首先创建一个数据库的表(如果不会点击这里数据库的复习
  2. 然后创建一个JAVA项目,并且添加驱动
    在这里插入图片描述
    在这里插入图片描述
  3. 实现JDBC的操作
public static void main(String[] args) throws Exception{
	//注册连接
	DriverManager.registerDriver(new com.mysql.jdbc.Driver());
	//获取链接
	Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
	//得到执行SQL语句的对象Statement
	Statement stmt = conn.createStatement();
	//执行SQL语句,并返回结果
	ResultSet rs = stmt.executeQuery("select * from newteacher");
	//处理结果
	while(rs.next()){//next()是游标,用来指向数据库表的行
		System.out.println(rs.getObject(1));
		System.out.println(rs.getObject(2));
		System.out.println(rs.getObject(3));
		System.out.println("--------------");
	}
	//关闭资源
	rs.close();
	stmt.close();
	conn.close();
}

JDBC常用的类和接口详解

  1. java.sql.Drivermanager类 : 创建连接
    • 注册驱动
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());不建议使用
      • 原因有2个:
        导致驱动被注册2次。(查看底层代码)
        强烈依赖数据库的驱动jar(没有链接就不行)
      • 解决办法:
        Class.forName(“com.mysql.jdbc.Driver”);//反射原理
    • 与数据库建立连接
      • static Connection getConnection(String url, String user, String password)
        试图建立到给定数据库 URL 的连接。
      • getConnection(“jdbc:mysql://localhost:3306/day06”, “root”, “root”);
        • URL:SUN公司与数据库厂商之间的一种协议。
        • jdbc:mysql://localhost:3306/day06
          协议 子协议 IP :端口号 数据库
        • mysql: jdbc:mysql://localhost:3306/day14 或者 jdbc:mysql:///day14(默认本机连接)
        • oracle: jdbc:oracle:thin:@localhost:1521:sid
        • Properties info = new Properties();//要参考数据库文档
          info.setProperty(“user”, “root”);
          info.setProperty(“password”,“root”);
          getConnection(String url, Properties info)
        • getConnection(String url)
          DriverManager.getConnection(“jdbc:mysql://localhost:3306/day14?user=root&password=root”);
  2. java.sql.Connection接口:一个连接
    • 接口的实现在数据库驱动中。所有与数据库交互都是基于连接对象的。
      Statement createStatement(); //创建操作sql语句的对象
  3. java.sql.Statement接口: 操作sql语句,并返回相应结果的对象
    • 接口的实现在数据库驱动中。用于执行静态 SQL 语句并返回它所生成结果的对象。
    • ResultSet executeQuery(String sql) 根据查询语句返回结果集。只能执行select语句。
    • int executeUpdate(String sql) 根据执行的DML(insert update delete)语句,返回受影响的行数。
    • boolean execute(String sql) 此方法可以执行任意sql语句。返回boolean值,表示是否返回ResultSet结果集。仅当执行select语句,且有返回结果时返回true, 其它语句都返回false;
  4. java.sql.ResultSet接口: 结果集(客户端存表数据的对象)
    • 封装结果集。

      • 提供一个游标,默认游标指向结果集第一行之前。
      • 调用一次next(),游标向下移动一行。
      • 提供一些get方法。
    • 封装数据的方法
      Object getObject(int columnIndex); 根据序号取值,索引从1开始
      Object getObject(String ColomnName); 根据列名取值。

    • 将结果集中的数据封装到javaBean中
      java的数据类型与数据库中的类型的关系
      JAVA: 数据库:
      byte | tityint
      short | smallint
      int | int
      long | bigint
      float | float
      double | double
      String | char varchar
      Date | date

    • boolean next() 将光标从当前位置向下移动一行
      int getInt(int colIndex) 以int形式获取ResultSet结果集当前行指定列号值
      int getInt(String colLabel) 以int形式获取ResultSet结果集当前行指定列名值
      float getFloat(int colIndex) 以float形式获取ResultSet结果集当前行指定列号值
      float getFloat(String colLabel) 以float形式获取ResultSet结果集当前行指定列名值
      String getString(int colIndex) 以String 形式获取ResultSet结果集当前行指定列号值
      String getString(String colLabel) 以String形式获取ResultSet结果集当前行指定列名值
      Date getDate(int columnIndex);
      Date getDate(String columnName);
      void close() 关闭ResultSet 对象

    • 可移动游标的方法

      • boolean next() 将光标从当前位置向前移一行。
      • boolean previous() 将光标移动到此 ResultSet 对象的上一行。
      • boolean absolute(int row) 参数是当前行的索引,从1开始根据行的索引定位移动的指定索引行。
      • void afterLast() 将光标移动到末尾,正好位于最后一行之后。
      • void beforeFirst() 将光标移动到开头,正好位于第一行之前。
  5. 释放资源
    资源有限,要正确关闭。

```java
@Test
	public void test4() throws Exception{
		//获取连接Connection
		Connection conn = null;
		//得到执行sql语句的对象Statement
		Statement stmt = null;
		//执行sql语句,并返回结果
		ResultSet rs = null;
		try {
			//加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1?user=root&password=123456");
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select id,name,password,email,birthday form users");
			//处理结果 
			while(rs.next()){ 
				System.out.println(rs.getObject(1));
				System.out.println(rs.getObject(2));
				System.out.println(rs.getObject(3));
				System.out.println(rs.getObject(4));
				System.out.println(rs.getObject(5));
				System.out.println("-----------------");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//关闭资源
			if(rs!=null){
				try {
					rs.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				rs = null;
			}
			if(stmt!=null){
				try {
					stmt.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				stmt = null;
			}
			if(conn!=null){
				try {
					conn.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				conn = null;
			}
			
		}
	}
  1. SQL注入问题:preparedStatement
    preparedStatement:预编译对象, 是Statement对象的子类。
    特点:
    • 性能要高
    • 会把sql语句先编译
    • sql语句中的参数会发生变化,过滤掉用户输入的关键字。

在这里插入图片描述

好了今天复习完了

发布了9 篇原创文章 · 获赞 8 · 访问量 386

猜你喜欢

转载自blog.csdn.net/qq_41816516/article/details/104619924