zoukankan      html  css  js  c++  java
  • JDBC_ResultSet结果集用法_游标原理_关闭连接问题

    依次关闭使用对象及连接

    ResultSet---Statement--Connection
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    /**
    * 测试ResultSet结果集的基本用法
    * @author Administrator
    */
    public class Demo004 {
    public static void main(String[] args) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection(
    "jdbc:mysql://localhost/testjdbc", "root", "");
    String sql = "select id,username,pwd from t_user where id>?";//?占位符
    ps = conn.prepareStatement(sql);
    ps.setObject(1, 3);//把id大于3的记录都取出来
    rs = ps.executeQuery();
    while (rs.next()) {
    System.out
    .println(rs.getInt("id") + "--"
    + rs.getString("username") + "--"
    + rs.getString("pwd"));
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (rs != null) {
    try {
    rs.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (ps != null) {
    try {
    ps.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (conn != null) {
    try {
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    }
    }

  • 相关阅读:
    001 windows下如何生成公钥和私钥
    函数基础
    各种推导式
    MySQL误删数据
    kafka 学习笔记
    Nginx 功能
    Nginx 到底可以做什么
    Nginx 到底可以做什么
    Shell的18条常用命令整理
    超详细 Nginx 极简教程
  • 原文地址:https://www.cnblogs.com/qhcyp/p/10452721.html
Copyright © 2011-2022 走看看