本例子程序是根据马士兵老师所讲+自己的注释。写的比较全面,特别是最后释放资源的代码。

 package com.ayang.jdbc;

 import java.sql.*;

 public class TestJDBC {

     public static void main(String[] args)  {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
//1、注册驱动
//new oracle.jdbc.driver.OracleDriver();
Class.forName("oracle.jdbc.driver.OracleDriver");
//2、建立连接
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","scott", "root");
//3、创建语句
stmt = conn.createStatement();
//4、执行语句
rs = stmt.executeQuery("select * from dept2");
System.out.println("deptno ||dname ||location");
//5、处理结果
while(rs.next()){
System.out.println(rs.getString("deptno")+" "+rs.getString("dname")+" "+rs.getString("loc"));
}
}catch (ClassNotFoundException e) {
System.out.println("未正常加载jdbc驱动");
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace(); //log for java }finally{
//6、释放资源
try {
if(rs != null){ //如果rs没有初始化,这肯定报exception,故判断一下
rs.close();
rs = null; //垃圾回收随时可以回收
}if(stmt != null){
stmt.close();
stmt = null;
}if(conn != null){
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
} }
} }

oracle_jdbc_Query的更多相关文章

随机推荐

  1. tred_extract_EDED_new

    # -*- coding:utf-8 -*- import re ''' 适应新版本 ''' year='17a'#用户自定义 ss='./data/'#根目录 filename = ss+'EDED ...

  2. PrefixHeader.pch 在工程中的使用

    1)  新建一个pch文件 2) 在 工程 Build Settings 中搜索 header  将Precompile Prefix Header 置为YES 2) 选中pch文件, 将右侧相对路径 ...

  3. POJ3258 River Hopscotch 2017-05-11 17:58 36人阅读 评论(0) 收藏

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13598   Accepted: 5791 ...

  4. HDU2553 N皇后问题 2016-07-24 13:56 283人阅读 评论(0) 收藏

    N皇后问题 Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是, ...

  5. OpenGl 坐标转换 (转载)

    OpenGl 坐标转换 (转载) 1. OpenGL 渲染管线 OpenGL渲染管线分为两大部分,模型观测变换(ModelView Transformation)和投影变换(Projection Tr ...

  6. hdu 4950 打怪

    http://acm.hdu.edu.cn/showproblem.php?pid=4950 给定怪兽血量h,你攻击力a,怪物回血力b,你攻击k次要休息一次,问能否杀死怪兽 特判一次打死怪兽的情况和第 ...

  7. hdu 4957 贪心破木桶接水大trick

    http://acm.hdu.edu.cn/showproblem.php?pid=4957 拿n只破的木桶去接水,每只木桶漏水速度为a[i],最后要得到b[i]单位的水,自来水的出水速度为V,木桶里 ...

  8. cxgrid动态显示行号

    uses cxLookAndFeelPainters; type TMyCxGrid = class(TObject)    class procedure DrawIndicatorCell(    ...

  9. Python学习-16.Python中的错误处理

    虽然叫错误,但跟 C# 中的异常是一回事.只不过 Python 中叫错误(Error)而 C# 中叫异常(Exception). 先手工产生一个异常: file = open('','r') 上面一句 ...

  10. 使用redis实现【统计文章阅读量】及【最热文章】功能

    1.视图函数 # 不需要登录装饰器,匿名用户也可访问def article_detail(request, id, slug): # print(slug,id) article = get_obje ...