dao--1.jdbc

xiaoxiao2021-02-28  109

1      概述

1.1  jdbc是什么?

         JDBC(Java DataBase Connectivity,java数据库连接),简单说:就是可以直接通过java语言,去操作数据库。                

         jdbc是一套标准,它是由一些接口与类组成的。

        

1.2  构成

                    

        

 

1.3     什么是驱动?

         两个设备要进行通信,满足一定通信数据格式,数据格式由设备提供商规定,设备提供商为设备提供驱动软件,

         通过软件可以与该设备进行通信

 

 

2      连接数据库

2.1  开发步骤

 

2.2  DriverManager类

2.2.1  注册驱动 (只做一次)

Class.forName(“com.mysql.jdbc.Driver”);

DriverManager.registerDriver(com.mysql.jdbc.Driver);

System.setProperty(“jdbc.drivers”,“com.mysql.jdbc.Driver”);

 

2.2.2  创建数据库连接

String url = "jdbc:mysql:///one"

    String username = "root";

    String password = "root";

Connection conn = DriverManager.getConnection(url,username,password);

  

2.2.3  URL

 

常用数据库URL地址的写法:

         Oracle写法:jdbc:oracle:thin:@localhost:1521:sid

         MySql—jdbc:mysql://localhost:3306/user

                   简写形式: jdbc:mysql:///user

 

常用属性:useUnicode=true&characterEncoding=UTF-8

 

 

2.3  定义sql语句

 

 

2.4  Connection接口

         它代表的是一个连接对象。简单说,就是我们程序与数据库连接。

2.4.1  创建命令对象

 

2.4.2  操作事务

2.5  Statement接口

2.5.1  命令对象执行sql语句

 

 

2.5.2  批处理操作

 

2.6  ResultSet接口

2.6.1  next()方法

         publicboolean next();

         用于判断是否有下一条记录。如果有返回true,并且让游标向下移动一行。如果没有返回false.

 

2.6.2  getXxx()

 

2.6.3  处理执行结果

 

 

2.7  释放资源

         Jdbc程序运行完后,切记要释放程序在运行过程中,创建的那些与数据库进行交互的对象,这些对象通常是ResultSet, Statement和Connection对象。

         特别是Connection对象,它是非常稀有的资源,用完后必须马上释放,如果Connection不能及时、正确的关闭,极易导致系统宕机。Connection的使用原则是尽量晚创建,尽量早的释放。

         为确保资源释放代码能运行,资源释放代码也一定要放在finally语句中。

 

3      数据类型

3.1  常用数据类型转换表

 

3.2  日期、大数据

 

public class MyTextTest { //存储 @Test publicvoid save() throws SQLException, FileNotFoundException { Stringsql = "insert into mytext values(null,?)"; //1.获取Connection Connectioncon = JdbcUtils.getConnection(); //2.获取PreparedStatement PreparedStatementpst = con.prepareStatement(sql); //3.插入值 Filefile = new File("D:\\java1110\\workspace\\day17_3\\a.txt"); FileReaderfr = new FileReader(file); pst.setCharacterStream(1,fr, (int) (file.length())); pst.executeUpdate(); //4.释放资源 pst.close(); con.close(); } //获取 @Test publicvoid get() throws SQLException, IOException { Stringsql = "select * from mytext where id=?"; //1.获取Connection Connectioncon = JdbcUtils.getConnection(); //2.获取PreparedStatement PreparedStatementpst = con.prepareStatement(sql); pst.setInt(1,1); //3.得到结果集 ResultSetrs = pst.executeQuery(); //4.遍历结果集 if(rs.next()) { Readerr = rs.getCharacterStream("content"); FileWriterfw = new FileWriter("d:/笔记.txt"); intlen = -1; char[]ch = new char[1024 * 100]; while((len = r.read(ch)) != -1) { fw.write(ch,0, len); fw.flush(); } fw.close(); r.close(); } pst.close(); con.close(); } } public class MyBlobTest { //添加 @Test publicvoid save() throws SQLException, IOException { Stringsql = "insert into myblob values(null,?)"; //1.获取Connection Connectioncon = JdbcUtils.getConnection(); //2.获取PreparedStatement PreparedStatementpst = con.prepareStatement(sql); //3.插入值 Filefile = new File("D:\\java1110\\day17-jdbc\\视频\\3.jdbc快速入门.avi"); FileInputStreamfis = new FileInputStream(file); pst.setBinaryStream(1,fis, (int) (file.length())); introw = pst.executeUpdate(); if(row != 0) { System.out.println("插入成功"); } //4.释放资源 fis.close(); pst.close(); con.close(); } //获取 @Test publicvoid get() throws SQLException, IOException { Stringsql = "select * from myblob where id=?"; //1.获取Connection Connectioncon = JdbcUtils.getConnection(); //2.获取PreparedStatement PreparedStatementpst = con.prepareStatement(sql); pst.setInt(1,1); //3.得到结果集 ResultSetrs = pst.executeQuery(); //4.遍历结果集 if(rs.next()) { //System.out.println(rs.getInt("id")); InputStreamis = rs.getBinaryStream("content");// 得到的这个输入流它的源可以理解成就是数据库中的大二进制信息 FileOutputStreamfos = new FileOutputStream("d:/a.avi"); intlen = -1; byte[]b = new byte[1024 * 100]; while((len = is.read(b)) != -1) { fos.write(b,0, len); fos.flush(); } fos.close(); is.close(); } //5.关闭 rs.close(); pst.close(); con.close(); } }

 

 

转载请注明原文地址: https://www.6miu.com/read-70161.html

最新回复(0)