zoukankan      html  css  js  c++  java
  • JDBC学习小结

    JDBC学习小结

    一、JDBC:java语言操作数据库的一种技术(规范)。
    
    二、JDBC中的4个核心对象
        DriverManager类,注册驱动、建立连接对象,在java.sql.DriverManager;
        Connection接口,获取执行sql语句的对象,在java.sql.Connection;
        Statement接口,执行SQL语句,在java.sql.Statement;
            PreparedStatement接口,在java.sql.PreparedStatement; 实际开发中使用它
        ResultSet接口,客户端存表数据的对象,在java.sql.ResultSet;
        
    三、
        DriverManager类,注册驱动、建立连接对象,在java.sql.DriverManager;
            registerDriver(new com.mysql.jdbc.Driver());
            Class.forName("com.mysql.jdbc.Driver");
            
            getConnection(url, username, password);
            
        Connection接口,获取执行sql语句的对象,在java.sql.Connection;
            stmt = createStatement();
            
            pstmt = prepareStatement("select * from users where id = ? and name = ?");
            pstmt.getInt(1, 4);
            pstmt.getString(2, bruce);
            
        Statement接口,执行SQL语句,在java.sql.Statement;
            ResultSet executeQuery(sql); // 执行 select 语句
            int executeUpdate(sql);      // 执行 insert、 update、delete 语句
            boolean execute(sql);        // 仅当执行的是 select 语句,且有返回结果时返回true,其它语句都返回false。
            
            ResultSet executeQuery();
            int executeUpdate();
            boolean execute(); 
            
        ResultSet接口,客户端存表数据的对象,在java.sql.ResultSet;
            boolean next();    // 把游标向下移动一行
            
            int getInt(int columnIndex);   // 根据列号进行查找,列号从1开始(根据列的索引查找,索引从1开始)
            int getInt(String columnName); // 根据列名进行查找
            
            double getDouble();
            float getFloat();
            
            String getString();
            Date getDate();
            ...
            
        PreparedStatement接口,在java.sql.PreparedStatement;
        特点:安全高效,防止恶义SQL语法1、性能要高;PreparedStatement实例包含已编译的SQL语句,所以其执行速度要快于Statement对象。
            2、会把sql语句先编译。
            3、sql语句中的参数会发生变化,过滤掉用户输入的关键字。            
            
  • 相关阅读:
    【Static Program Analysis
    【Py-Github】根据条件筛选Github repo的例子
    【Static Program Analysis
    【Static Program Analysis
    【Static Program Analysis
    【Static Program Analysis
    【IEEE会议论文】格式规范问题
    搜索引擎(3)——查询理解——不可省词
    搜索引擎(2)—— 查询理解 —— 分词
    搜索引擎(1)—— 概述与功能架构
  • 原文地址:https://www.cnblogs.com/chenmingjun/p/8972487.html
Copyright © 2011-2022 走看看