zoukankan      html  css  js  c++  java
  • jsp/servlet学习笔记(核心编程)mysql部分

    第十七章 访问JDBC
    (1)载入JDBC驱动程序
    需要在Class.forName方法中指定数据库驱动程序的类型
    try{
    Class.forName("connect.jdbc.driver.OracleDriver");
    }catch(ClassNotFountException){
    System.err.println("");
    }

    (2)定义链接URL(指定协议,主机名、端口和数据库名)
    String host="dbhost.yourcompany.com";
    String dbName = "someName";
    String port = "1234";

    String orcalURL = "jdbc:oracl:thin:@" + host +":" + port + ":" + dbName;

    (3)建立链接
    String username = "qi";
    String password = "xiaoqi";
    try{
    ConnectManager.getConnection(orcalURL,username,password);
    }catch(SQLException e){
    System.err.println("");
    }
    (4)创建Statement对象
    创建Statement对象才能向数据库发送查询和命令
    Statement statement = connect.createStatement();
    允许在同一个链接中打开多个并行Statement对象

    (5)执行查询或更新
    有了Statement对象后就可以使用execute,executeQuery,executeUpdate或executeBatch方法发送Sql语句到数据库
    String sql = "";
    ResultSet result = Statement.executeQuery(sql);

    executeQuery 返回查询结果
    executeUpdate 返回受影响的行数
    executeBatch 将一组命令作为一个单元执行返回一个数组 每个存储每次命令的更新计数
    setQueryTimeout 抛出异常时,等待处理结果的时间
    getMaxRows/setMaxRows 确定结果集合的最大行数

    (6)处理结果
    返回一个ResultSet集合。表示一系列的行和列,可以调用next和各种getXxx方法处理行和列
    最简单的方式就是使用next在表中移动 每次移动一行
    在一行之内提供各种getXxx方法,都已列名或列索引为参数,以各种不同的java类型返回(getInt,getString等)。如果使用列索引,注意索引都是从1开始。
    while(result.next) {
    System.out.println(
    result.getstring(1) + " " +
    result.getstring(2) + " " +
    result.getstring("firstName") + " "
    result.getstring("lastName")
    );
    }
    不建议使用列索引,要使用列名 防止出错

    ResultSetMetaData对象可以给出列的数目与名称

    (7)关闭链接

    两种实现:jdbc-odbc桥接器 纯java实现(推荐)


    第十八章 配置Mysql和Oracle9i
    第十九章 Html表单创建与处理

  • 相关阅读:
    使用runOnUiThread更新UI
    Leetcode Symmetric Tree
    EBS 开发中如何动态启用和禁止请求(Current Request)的参数
    c 陷阱与缺陷(一)
    钟浩荣战胜病魔,不负众望重踏传智播客!
    【原创】分布式之elk日志架构的演进
    【强烈谴责】博客园园友随意抄袭他人文章并作为自己原创的行为
    【原创】研发应该懂的binlog知识(下)
    【原创】研发应该懂的binlog知识(上)
    【原创】一个线程oom,进程里其他线程还能运行吗?
  • 原文地址:https://www.cnblogs.com/luckyQi/p/6782569.html
Copyright © 2011-2022 走看看