Latest News
Đẩy log exception của spring boot lên elasticsearch
  • About
  • EmEditor
  • Register Google Adsense

Love Coding

Note anything I want

  • Home
  • Web Development
    • HTML
    • Javascript
    • jQuery
    • CSS
    • PHP
    • ASP
    • JSP
    • Fix Bug
  • Other Development
    • Desktop Java
    • Mobile J2ME
    • VBS
  • Operating System
    • Windows
    • Linux
  • Database
    • MySQL
    • Oracle
  • Software
Home » Java » Oracle » Love Coding: Java connect to Oracle, select data

Java connect to Oracle, select data

HuyPV
Add Comment
Java , Oracle
Tuesday, August 31, 2010
package api;

import java.sql.*;

public class TestOracle {
    public static void main(String args[]) {
        Connection connection;
        Statement stmt;
        ResultSet rs;
      
        try {
            // Load the JDBC driver
            String driverName = "oracle.jdbc.driver.OracleDriver";
            Class.forName(driverName);

            // Create a connection to the database (get connection info from $ORACLE_HOME\NETWORK\ADMIN\tnsnames.ora)
            String serverName = "VTT_VE_HUYPV";
            String portNumber = "1521";
            String sid = "orcl";
            String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber
                    + ":" + sid;
            String username = "portal";
            String password = "abc";
            connection = DriverManager.getConnection(url, username, password);
          
            stmt = connection.createStatement();
            int offset = 0;
            int limit = 1000;
            String sql = "SELECT * FROM (SELECT rownum rn, a.* from HR.COUNTRIES a) WHERE rn > "+String.valueOf(offset)+" AND rn <= "+String.valueOf(offset + limit);
          
            // Create a scrollable ResultSet.
            stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery(sql);

            // Point to the last row in resultset.
            rs.last();

            // Get the row position which is also the number of rows in the
            // ResultSet.
            int rowcount = rs.getRow();

            System.out.println("Total rows for the query: " + rowcount);

            // Reposition at the beginning of the ResultSet to take up rs.next()
            // call.
            rs.beforeFirst();
          
            while (rs.next()) {
                System.out.println(rs.getString(2) + " = " + rs.getString(3));
            }
        } catch (ClassNotFoundException e) {
            // Could not find the database driver
            System.out.println("404 DB Driver");
        } catch (SQLException e) {
            // Could not connect to the database
            e.printStackTrace();
        }
      
      
    }
}
Tweet
Java connect to Oracle, select data Title: Java connect to Oracle, select data
Description: package api; import java.sql.*; public class TestOracle {     public static void main(String args[]) {         Connection connection; ... ...
Rating: 4

No comments :

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments ( Atom )
Quảng cáo

Popular Posts

  • VBS - Upload file HTTP Post
    Source: http://www.ericphelps.com/scripting/samples/Reference/Web/HTTP_POST.txt   Sub Upload(strUploadUrl, strFilePath, strFileField, strD...
  • Add other collapse div to your forum
    Collapse <div style="height: 16px; padding-right: 4px; font-weight: bold;" class="blockhead"> <span style=...
  • Use the YouTube API with PHP
    Process and integrate data from YouTube into your PHP application with PHP's SimpleXML extension Summary:   The YouTube vide...
  • Check Laravel version
    Đối tác bảo đang code và dùng Laravel, giờ muốn biết version Laravel tương ứng là bao nhiêu để xem mà code theo. Làm sao check? Cách 1: Sử...
  • Hàm chuyển từ ngày dương lịch sang âm lịch (PHP)
    <?php function INT($d) {     return floor($d); } function jdFromDate($dd, $mm, $yy) {     $a = INT((14 - $mm) / 12);     $y = $yy + 4800 ...
  • Character Set và Collation trong MySQL là gì? Tạo DB thì để UTF8_GENERAL_CI hay UTF8_BIN
    Character Set là một tập các ký tự và dạng số hóa của các ký tự đó Collation là một tập các luật để so sánh các xâu được sinh ra từ các ký ...
  • Trong laravel thì queue:work với queue:listen khác nhau thế nào?
    Trong laravel thì queue:work với queue:listen đều chạy jobs trong queue. Vậy 2 cái này cũng phải có gì đó khác nhau chứ, nếu ko thì nó là ...
  • mysql_error: Undeclared variable: INF
    mysql_error: Undeclared variable: INF PHP Code: $limit = 10; $page = isset($_GET['p']) ? $_GET['p'] : 1; $offset = ($pa...
  • git checkout file from other branch
    Đang làm trên nhánh dev. Muốn lấy 1 file, ví dụ: xxx/helper.php ở trên nhánh master về dev thì làm thế nào? Cách 1: nông dân nhiều bước nh...
  • Download media files (video, audio) from VnExpress.Net
    Download media files (video, audio) from VnExpress.Net Example: http://vnexpress.net/GL/Vi-tinh/Giai-tri/2010/02/3BA18A0E/page_4.asp htt...
Back to top!
Copyright 2010 Love Coding - All Rights Reserved Design by Newbie_PC - Powered by Blogger