What good tutorial Java programmers interpretation is JDBC

Introduction to JDBC - JDBC (Java DataBase Connectivity, java Database Connectivity) is a Java API for executing SQL statements can provide unified access to multiple relational database that consists of a set written in Java classes and interfaces. JDBC provides a benchmark that allow you to build more advanced tools and interfaces that enable database developers to write database applications

- Java has a sturdy, safe, easy to use, easy to understand and can be automatically downloaded from the Internet and other characteristics, is to write database applications outstanding language. The method only dialogue between Java applications and a variety of different databases needed.

- JDBC you can use Java, such as Windows, Mac OS and various versions of UNIX on a variety of platforms.

- JDBC API library includes commonly used for each task associated with the database mentioned below.

Using JDBC step
    - connecting to the database.
    - Create a MySQL or SQL statements.
    - execute SQL or MySQL queries in the database.
    - View and modify records generated.

JDBC core components
 DriverManager:
    
 A list of such management database drivers. Using a communication protocol from the sub java application connection request matches the appropriate database drivers.

- Driver:
    * This interface handles the communication with the database server, we rarely interact directly with the Driver object. But using DriverManager objects to manage this type of object.

 Connection:
    
 The interface has all the methods used to contact the database. Communication context objects represents the connection, i.e., all communication with the database only by connecting the objects.

 Statement:
    
 Use this interface to create objects of SQL statements submitted to the database. In addition to performing stored procedures outside, some derived interface also accept parameters.

 ResultSet:
    
 After the execution of SQL query using Statement objects that hold data retrieved from the database. It as an iterator, allowing us to move its data.

 SQLException:
    
 any error such processing database application

JDBC Case Code
package com.qianfeng.demos;

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

public class Demo01 {

    static void main public (String [] args) throws a ClassNotFoundException, SQLException {
        // 1. load the driver
        // DriverManager.registerDriver (new new com.mysql.jdbc.Driver ());
        the Class.forName ( "com.mysql.jdbc. Driver ");
        String url =" jdbc:? MySQL: // localhost: 3306 / Students useSSL = false ";
        String the User =" root ";
        String password =" root ";
        // 2. get links and java database
        Connection DriverManager.getConnection = conn (url, the User, password);
        
        // 3. obtain statement object to execute SQL statements
        of statement stmt = conn.createStatement ();
        
        // 4. writing SQL statement
        String sql = "select * from shuihu ";
        
        // 5. execute the sql statement to get return results
        = Stmt.executeQuery the resultSet the ResultSet (SQL);
        
        // 6. The write cycle, is determined continuously and acquire content resultset in
        the while (ResultSet.next ()) {
            System.out.println (ResultSet.getObject (. 1)
                    + "\ T "ResultSet.getObject + (2)
                    +" \ T "+ ResultSet.getObject (. 3)
                    +" \ T "+ ResultSet.getObject (. 4)
                    +" \ T "+ ResultSet.getObject (. 5)
                    +" \ T "+ ResultSet.getObject (. 6));
        }
        
        // 7. The release resources
        ResultSet.close ();
        stmt.close ();
        conn.Close ();
    }
}

Guess you like

Origin blog.51cto.com/14256902/2423563