JDBC query example

  As the oldest and most basic specification for Java and database interaction, JDBC provides an interface to access the underlying database, and other ORM frameworks are built on the cornerstone of JDBC. Let's look at a basic JDBC query example:

package com.inspur.chinanet.point.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * JDBC instance
 *
 * @author wulinfeng
 * @version C10 April 28, 2018
 * @since SDP V300R003C10
 */
public class JDBCDeom
{
    private static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
    
    private static final String DB_URL = "jdbc:oracle:thin:@10.211.95.152:1521:testdba";
    
    private static final String USER = "wlf";
    
    private static final String PASSWORD = "wlf";
    
    public static void main(String[] args)
    {
        String sql = "select TITLE from t_ues_component_template";
        Connection conn = null;
        Statement stmt = null;
        try
        {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next())
            {
                String paramName = rs.getString("TITLE");
                System.out.println("TITLE: " + paramName);
            }
            rs.close();
        }
        catch (SQLException se)
        {
            se.printStackTrace ();
        }
        catch (Exception e)
        {
            e.printStackTrace ();
        }
        finally
        {
            try
            {
                if (stmt != null)
                {
                    conn.close();
                }
            }
            catch (SQLException se)
            {
            }
            try
            {
                if (conn != null)
                {
                    conn.close();
                }
            }
            catch (SQLException se)
            {
                se.printStackTrace ();
            }
        }
        
    }
}

  The execution result is as follows:

TITLE: Swipe ad component
TITLE: Recommended by famous experts
TITLE: Event subtitle
TITLE: Styled Title Component
TITLE: Round Robin Linked List
TITLE: Category recommendation
TITLE: Ad Block
TITLE: Redeem Card Benefits
TITLE: Book List Created
TITLE: Private recommendation
TITLE: Navigating business components
TITLE: null
TITLE: component test1
TITLE: Content Display
TITLE: Content Links
TITLE: Content button
TITLE: List of comments
TITLE: Book Catalog
TITLE: List of Book Reviews
TITLE: Content Description
TITLE: List of books that contain this book
TITLE: Vertical Content List_Listen
TITLE: Discover the home page
TITLE: Leaderboard Tab List
TITLE: famous classification node
TITLE: Listening to Books_Book Catalog
TITLE: Introduction to the book list
TITLE: Member Center Top Information
TITLE: Content button
TITLE: Personal Center Page - Personal Group Information
TITLE: rich text test
TITLE: Vertical list of books
TITLE: Member Home - Benefits Description
TITLE: Member home top status
TITLE: Stealing Books Homepage
TITLE: Listen to the book_Content display
TITLE: Zone List
TITLE: Personal Center Page - Personal Book Review
TITLE: Visitor List
TITLE: Listening to the book_Special recommendation
TITLE: Happy Reading Coffee_banner carousel
TITLE: Activity page subtitle
TITLE: Mixed image and text list
TITLE: Book Title Classification
TITLE: Similar recommendations
TITLE: Migu guesses official
TITLE: Free Homepage -- Voting Information
TITLE: Free manga waterfall
TITLE: Horizontal Content List
TITLE: Multi-picture display_Listen Book
TITLE: Recommended horizontal list of three covers
TITLE: Single recommended book list with avatar
TITLE: Book Review List_V7
TITLE: Book Review List_V7
TITLE: Bonus Book Page
TITLE: Book List Created
TITLE: Sweepstakes Card Benefits
TITLE: 鍐呭膐 Xuan Yao
TITLE: Favorite Book List
TITLE: List of single book lists (book details page)
TITLE: Lottery Pool
TITLE: Book List Book List
TITLE: User Style Adjustment
TITLE: Book List Ranking
TITLE: Editor's Recommended Book List
TITLE: Personal Center Page - Personal Book List
TITLE: Leaderboard
TITLE: List of leaderboards
TITLE: tipping record
TITLE: MY MISSION
TITLE: Content Media Carousel
TITLE: Do tasks to earn growth
TITLE: Introduction to Book List_V7
TITLE: Membership Floating Window
TITLE: Giveaway Book Return Page
TITLE: List of popular notes
TITLE: Popular Note Entry
TITLE: Steal Bookshelf
TITLE: Content Link_Listening Book
TITLE: On-demand discounts
TITLE: Personal Book Review List_V7
TITLE: Result page for book donation
TITLE: Personal Notes
TITLE: Member homepage book selection
TITLE: Voting by Authors of Joy Read Coffee
TITLE: My fan list_V7
TITLE: New user login bonus
TITLE: My Watchlist_V7
TITLE: Promo Coupon Claim (Third Party)
TITLE: Feature Recommendation_Listen Book
TITLE: Sliding focus
TITLE: Book Stealing Benefits
TITLE: Audiobook Details Page - Content Button
TITLE: Personal Center Page - Personal Information
TITLE: Image link
TITLE: Personal Center Page - Personal Entrance
TITLE: Personal Center Page - Recently Read

  In fact, adding, deleting, checking, and modifying is the same routine. The first step is to establish a database connection, the second step is to execute the SQL statement, and the third step is to process the query results. Why do we need an Object Relational Mapping (ORM) framework? Because the JDBC routine is too cumbersome, it needs such a tossing every time, and the third step is especially troublesome, and it is necessary to convert the found basic type into an object. Exception handling thrown after performing database operations is also a hassle. In order to reduce these repetitive routines, we have come up with an ORM framework to simplify the code. But JDBC is still the underlying specification, we still need to understand.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325085257&siteId=291194637