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表单创建与处理

  • 相关阅读:
    Python-Matplotlib 12 多图figure
    Python-Matplotlib 11 子图-subplot
    Python Day16
    Python Day15
    Python Day13-14
    Python Day12
    Python Day11
    Python Day9-10
    Python Day8
    Python Day8
  • 原文地址:https://www.cnblogs.com/luckyQi/p/6782569.html
Copyright © 2011-2022 走看看