• JDBC概念


    JDBC:java语言操作数据库的技术。(对象、方法)

    简介:


     

    JDBC:一套java访问数据库的接口,通用访问接口,可以访问扔和数据库

    常用API


     

    常见接口:

    Connection:数据库连接,只有获得connection对象,才能连接数据库

    PreparedStatement:发送sql语句的工具,具备发送sql的功能(方法)

    ResultSet:结果集,接受数据库执行查询sql返回的查询结果

    Driver:驱动类,访问数据库的不同方式,不同的数据库会自动提供具体的接口实现类

    DriverManager:管理多个驱动类

    核心编程思想

    数据库客户端访问数据库的步骤

    1.连接数据库  用户名  密码

    2.打开发送SQL语句的工具

    3.准备SQL语句,发送SQL语句

    4.如果发送的查询语句,接收数据库的查询结果

    5.释放数据库资源  接收查询结果、发送SQL工具关闭、数据库连接断开

    JDBC访问数据库的步骤

    1.连接数据库 (获得数据库连接 Connection) 用户名 密码

    2.创建SQL发送工具 (PreparedStatement)

    3.准备SQL语句,  执行发送.PrepareStatement的execute方法

    4.如果发送的查询语句,接受查询结果  (ResultSet)

    5.释放资源  ResultSet  PrepareStatement  Connection

    JDBC案列


     

    思路

    JDBC操作

    1.发送增加语句

     1 package com.hg.jdbc;
     2 
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 import java.sql.PreparedStatement;
     6 
     7 public class JDBC01 {
     8     public static void main(String[] args) throws Exception {
     9         //1.加载驱动
    10         Class.forName("oracle.jdbc.OracleDriver");
    11         //2.获得数据库连接 Connection
    12         //DriverManager.getConnection("url","user","password");
    13         /**
    14          * url:jdbc远程访问oracle的路径
    15          * 格式:jdbc:oracle:thin:@数据库的ip地址:1521:orcl  (企业版为orcl)
    16          */
    17         Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "hr", "hr");
    18         System.out.println(conn);
    19         //3.创建发送sql的工具  PreparedStatement  
    20         //conn.preparedStatement("sql语句");
    21         String sql = "insert into t_person values(101,'画哥',1,19,'12345678910','6546@qq.com')";
    22         PreparedStatement pstm = conn.prepareStatement(sql);
    23         //4.准备sql,发送sql
    24         //executeUpdate:发送sql语句,int代表影响数据库数据的行数
    25         int i = pstm.executeUpdate();
    26         System.out.println(i);
    27         //5.如果是查询,接受查询结果,并处理
    28         
    29         //6.释放资源  ResultSet  PreparedStatement Connection  先打开的后关闭
    30         if(pstm!=null) {
    31             pstm.close();
    32         }
    33         if(conn!=null) {
    34             conn.close();
    35         }  
    37     }
    38 }

    2.接收结果ResultSet

    1.next()---移动resultset游标,下移一位,并返回boolean类型确定是否有值

    2.getXxxx(“查询结果列名”)---获得游标指向当前行,列名对应的数据

    3.getXxxx(序列号)---获得游标指向的当前行,序列号对应的数据

     1 Class.forName("oracle.jdbc.OracleDriver");
     2         
     3         Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "hr","hr");
     4         
     5         System.out.println(conn);
     6         String selectSQL = "select id,name,sex,age from t_person where id=100";
     7         
     8         PreparedStatement pstm = conn.prepareStatement(selectSQL);
     9         
    10         //存放查询结果
    11         ResultSet rs = pstm.executeQuery(); //
    12         
    13         if(rs.next()) {
    14             int id = rs.getInt("id");
    15             String name = rs.getString("name");
    16             int sex = rs.getInt("sex");
    17             int age = rs.getInt("age");
    18             
    19             System.out.println(id);
    20             System.out.println(name    );
    21             System.out.println(sex);
    22             System.out.println(age);
    23         }
    24         
    25         if(rs!=null) rs.close();
    26         if(pstm!=null) pstm.close();
    27         if(conn!=null)    conn.close();

       

  • 相关阅读:
    NSCalendar
    保护连接字符串
    System.Runtime.InteropServices.COMException: 拒绝访问.
    Windows 7 初体验
    Sql语句收藏
    因为WMI配置,无法执行Sql Server 系统配置检查器的解决办法
    url带中文参数显示乱码的问题
    想建立一个好的团队,有意者加我
    庆祝一下,早上的帖子上了24小时排行第三
    C/S结构和b/s结构的比较
  • 原文地址:https://www.cnblogs.com/lhl0131/p/12487364.html
Copyright © 2020-2023  润新知