`
kylinsoong
  • 浏览: 240674 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Thinking in JDBC

阅读更多

This blog will beas on a series of examples to account for each viewpoint which will list below.

I. Overview of JDBC

      JDBC defines how a Java program can communicate with a database. JDBC API provides two packages they are java.sql and javax.sql . By using JDBC API, you can connect virtually any database, send SQL queries to the database and process the results. JDBC API provides interfaces and classes to work with databases. Connection interface encapsulates database connection functionality, Statement interface encapsulates SQL query representation and execution functionality and ResultSet interface encapsulates retrieving data which comes from execution of SQL query using Statement. The following are the basic steps to write a JDBC program:

      1. Import java.sql and javax.sql packages
      2. Load JDBC driver
      3. Establish connection to the database using Connection interface
      4. Create a Statement by passing SQL query
      5. Execute the Statement
      6. Retrieve results by using ResultSet interface
      7. Close Statement and Connection

Code Demo:

public class JDBCTest {

	public static void main(String[] args) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet r = null;
		final String driver = "oracle.jdbc.OracleDriver";
		final String url = "jdbc:oracle:thin:@//192.168.1.107:1521/oracl";
		final String user = "IPCUSER";
		final String passwd = "tibco";
		
		try {
			Class c = Class.forName(driver);
			Driver d = (Driver) c.newInstance();
			DriverManager.registerDriver(d);
			conn = DriverManager.getConnection(url, user, passwd);
			stmt = conn.createStatement();
			r = stmt.executeQuery("SELECT USER_TABLES.TABLE_NAME FROM USER_TABLES");
			while(r.next()) {
				System.out.println(r.getString(1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			r.close();
			stmt.close();
			conn.close();
		}	
	}
}

 

 Demo Code will extrct all table name of current user

 

II. Does "Connection.setAutoCommit(false)" mean "begin a transaction?"

      A transaction is simply an operation that is irreversibly completed. By default, JDBC connections start in autocommit mode. This means that every executed statement is treated as a separate transaction. There are occasions where this default behavior is undesirable. Having every statement commited to the database can reduce performance. In addition, a set of statements may have dependencies such that previous statements should be undone if a succeeding statement fails. This is why the JDBC API provides the

Connection.setAutoCommit() method.

You can turn off the default autocommit behavior by calling this method with an argument of false.

All statements executed afterward will not take effect until you call commit().

Therefore, the first call of setAutoCommit(false), and each call of commit() implicitly mark the start of a transaction. Transactions can be undone before they are committed by calling rollback().  You will typically rollback a transaction when one of its constituent statements fails

 

III. ROWNUM Pseudocolumn

1. rownum is a pseudo column. It numbers the records in a result set. The first record that meets the where criteria in a select statement is given rownum=1, and every subsequent record meeting that same criteria increases rownum.

2. Conditions testing for ROWNUM values greater than a positive integer are always false. For example, this query returns no rows:

select * from migrationproclog where rownum > 1

The first row fetched is assigned a ROWNUM of 1 and makes the condition false. The second row to be fetched is now the first row and is also assigned a ROWNUM of 1 and makes the condition false. All rows subsequently fail to satisfy the condition, so no rows are returned.

 

IV. Query with index is 50 times faster than witout index

      I will give a test statistic to demostrate this viewpoint. In this test there are more then 1 000 000 data exists in table ORDERS, I have query the 1 000 000th row two times, the first time without index in num column, and the second times create index in num column, as following:

Query SQL Statement:

SELECT num, extract(data, '/') FROM ORDERS WHERE num = 1000000

Create Index Statement:

create index orders_num_index on orders(num);

Query Time:

without index Time: 178953 milliseconds
     with index Time: 3297 milliseconds

 

 So if our application need query database frequently we should consider create index to ehance query performance. 

 

V. Use pseudo ROWNUM to implement Pagination should be careful

      It's really frequent that we need to page query, for example the following demo show query Orders rows from 1 000 000 to 1 500 000, usually we can achieve this goal through rownum, I will give my Pagination SQL statement and the query time directly:

SELECT r, target FROM (SELECT ROWNUM r, extract(data, '/') target FROM orders ) WHERE r >= 1 AND r < 500000
Time: 218 milliseconds
SELECT r, target FROM (SELECT ROWNUM r, extract(data, '/') target FROM orders ) WHERE r >= 500000 AND r < 1000000
Time: 513610 milliseconds
SELECT r, target FROM (SELECT ROWNUM r, extract(data, '/') target FROM orders ) WHERE r >= 1000000 AND r < 1500000
Time: 1429547 milliseconds

 

SELECT r, extract(data, '/') FROM (SELECT ROWNUM r, data FROM orders ) WHERE r >= 1 AND r < 500000
Time: 235 milliseconds 
SELECT r, extract(data, '/') FROM (SELECT ROWNUM r, data FROM orders ) WHERE r >= 500000 AND r < 1000000
Time: 79578 milliseconds
SELECT r, extract(data, '/') FROM (SELECT ROWNUM r, data FROM orders ) WHERE r >= 1000000 AND r < 1500000
Time: 130015 milliseconds

 

SELECT r, extract(data, '/') FROM (SELECT ROWNUM r, data FROM orders WHERE ROWNUM < 500000) WHERE r >= 1
Time: 203 milliseconds
SELECT r, extract(data, '/') FROM (SELECT ROWNUM r, data FROM orders WHERE ROWNUM < 1000000) WHERE r >= 500000
Time: 73204 milliseconds
SELECT r, extract(data, '/') FROM (SELECT ROWNUM r, data FROM orders WHERE ROWNUM < 1500000) WHERE r >= 1000000
Time: 124391 milliseconds

 

The above sql statement illustrate 3 different pagination methods and the query time is very different, so we should be careful when we use ROWNUM to implement Pagination .

 

1
0
分享到:
评论

相关推荐

    thinking in java 第四版

    《Thinking in Java》是Bruce Eckel的经典之作,第四版更是汇集了Java编程的精髓,适合初学者和有经验的开发者深入学习。这本书以其全面、深入浅出的讲解方式深受程序员喜爱,尤其对于想要理解Java语言本质的人来说...

    thinking in java word版

    《Thinking In Java》是由Bruce Eckel编写的著名Java编程书籍,中文版的出现为中文读者提供了深入学习Java的宝贵资源。这本书旨在引导初学者和有经验的程序员掌握Java语言,而不仅仅是平台相关的理论。它涵盖了从...

    thinking in java 代码

    《Thinking in Java》是Bruce Eckel的经典编程教材,它深入浅出地介绍了Java语言的核心概念和技术。这本书以其全面性、深度和清晰的讲解而受到广大程序员的推崇,是Java学习者的必读之作。这里我们主要关注的是其源...

    Thinking in Java 4th edition

    《Thinking in Java》是Bruce Eckel的经典之作,第四版更是被广大Java开发者视为学习和进阶的必备书籍。这本书深入浅出地介绍了Java语言的核心概念和技术,覆盖了从基础到高级的主题,旨在培养读者“思考”像Java...

    Thinking in Java 中文第四版+习题答案

    15.7.3 JDBC API为何如何复杂 15.8 远程方法 15.8.1 远程接口概念 15.8.2 远程接口的实施 15.8.3 创建根与干 15.8.4 使用远程对象 15.8.5 RMI的替选方案 15.9 总结 15.10 练习 第16章 设计范式 16.1 范式的概念 ...

    thinking in java 3rd edition教材源码和补充jar包

    在《Thinking in Java》的补充jar包中,可能包含了书中引用到的多种第三方开源库,比如用于数据库操作的JDBC库,用于图形用户界面(GUI)构建的Swing或JavaFX,用于XML解析的DOM或SAX库,以及用于网络编程的Netty...

    Thinking in Java(pdf+chm).zip

    《Thinking in Java》是 Bruce Eckel 的经典著作,它深入浅出地介绍了Java编程语言,是许多Java初学者和进阶者的重要参考书籍。这本书涵盖了Java的基础知识、面向对象编程概念、高级特性以及并发编程等多个方面,...

    Thinking in Java, 3rd ed. 中文+英文chm版

    《Thinking in Java》是Bruce Eckel的经典编程教材,特别是第三版,它深入浅出地介绍了Java编程语言的核心概念和技术。这本书对于初学者和有经验的程序员来说都是宝贵的资源,因为它不仅讲解了Java的基础,还涵盖了...

    Thinking in Pattern

    《Thinking in Pattern》一文,从其标题来看,显然是关于设计模式的讨论。设计模式是软件工程中的一个重要概念,它代表了在特定上下文中解决常见问题的最佳实践。这些模式是经验丰富的开发者们在长期实践中总结出的...

    Thinking in Java简体中文(全)

    15.7.3 JDBC API为何如何复杂 15.8 远程方法 15.8.1 远程接口概念 15.8.2 远程接口的实施 15.8.3 创建根与干 15.8.4 使用远程对象 15.8.5 RMI的替选方案 15.9 总结 15.10 练习 第16章 设计范式 16.1 范式的概念 ...

    java编程思想thinking in java

    以上只是《Java编程思想》中部分核心知识点的概述,实际书籍中涉及的内容更为丰富,包括网络编程、数据库连接、GUI编程、Swing、JDBC、设计模式等多个方面,是一本全面而深入的Java学习指南。通过阅读和实践书中的...

    thinking in java 答案

    《Thinking in Java》是Bruce Eckel的经典Java编程书籍,它深入浅出地讲解了Java语言的核心概念和技术。这本书以其详尽的解释、丰富的示例和深入的理论实践深受程序员喜爱。"Thinking in Java 答案"则为读者提供了书...

    Thinking in Enterprise Java

    《Thinking in Enterprise Java》是一本深入探讨企业级Java开发的权威著作。这本书涵盖了广泛的Java技术领域,旨在帮助开发者全面理解并掌握在企业环境中构建高效、可扩展和可靠的软件系统的关键概念和技术。 首先...

    Thinking in Java 4th Edition

    ### 《Thinking in Java 第四版》核心知识点详解 #### 一、书籍概述与作者介绍 《Thinking in Java 第四版》是由 Bruce Eckel 编写的一本经典 Java 编程指南。本书作为 Java 学习的经典之作,在全球范围内广受好评...

    Thinking in java 1

    《Thinking in Java》是Java编程领域的一本经典教材,由Bruce Eckel撰写,被誉为学习Java的最佳书籍之一。这本书深入浅出地介绍了Java语言的核心概念、语法以及编程思想,旨在帮助读者不仅掌握Java语言本身,更理解...

    Thinking in Java, 3rd ed.

    《Thinking in Java》是Bruce Eckel的经典著作,第三版涵盖了Java编程语言的广泛主题,旨在帮助程序员深入理解Java的本质和哲学。这本书不仅讲解了语言的基础,还探讨了高级特性和设计模式,是学习和进阶Java开发的...

    Thinking in Java 3rd Edition

    《Thinking in Java 3rd Edition》是一本深受程序员喜爱的经典Java学习教材,由 Bruce Eckel 撰写。这本书以其深入浅出的讲解方式和全面的内容覆盖,成为了Java初学者和资深开发者的重要参考书籍。其中文版的出版,...

    thinking in java 电子中文版

    《Thinking in Java》是一本由Bruce Eckel编著的经典Java编程教材,中文版为国内Java开发者提供了方便的学习资源。这本书以其深入浅出的讲解方式,全面覆盖了Java语言的基础到高级概念,是许多程序员入门和提升Java...

    Thinking in Enterprise Java(良好排版)

    《Thinking in Enterprise Java》这本书深入探讨了企业级Java编程的关键概念和技术。从网络编程到数据库连接,再到服务器端页面开发,该书为读者呈现了全面的企业级Java应用开发视角。 首先,书中介绍了企业编程的...

Global site tag (gtag.js) - Google Analytics