Getting Started with Jdbc

Source: Internet
Author: User
Tags driver manager

First, the most straightforward definition of JDBC:

the technique of sending SQL statements using Java Code (Programs) is the jdbc technology

Ii. Prerequisites for JDBC to send SQL

To connect to the database server:

    1. The IP address of the database
    2. Port
    3. Database user Name
    4. Password
/*** JDBC Connection database *@authorAPPle **/ Public classDemo1 {//URL of the connection database    PrivateString url = "Jdbc:mysql://localhost:3306/day01"; //JDBC Protocol: Database sub-protocol: Host: Port/Connected Database//    PrivateString user = "root";//User name    PrivateString password = "root";//Password        /*** First Method *@throwsException*/@Test Public voidTest1 ()throwsexception{//1. Creating a Driver class objectDriver Driver =NewCom.mysql.jdbc.Driver ();//New version//Driver Driver = new Org.gjt.mm.mysql.Driver ();//older versions//set user name and passwordProperties props =NewProperties (); Props.setproperty ("User", user); Props.setproperty ("Password", password); //2. Connect to the database and return to the Connection objectConnection conn =driver.connect (URL, props);    SYSTEM.OUT.PRINTLN (conn); }        /*** Use the Driver Manager class to connect to the database (registered two times, not necessary) *@throwsException*/@Test Public voidTest2 ()throwsexception{Driver Driver=NewCom.mysql.jdbc.Driver (); //Driver driver2 = new Com.oracle.jdbc.Driver (); //1. Registering a driver (multiple drivers can be registered)drivermanager.registerdriver (driver); //Drivermanager.registerdriver (DRIVER2); //2. Connect to a specific databaseConnection conn =drivermanager.getconnection (URL, user, password);            SYSTEM.OUT.PRINTLN (conn); }        /*** (This is the recommended way to connect to the database) * recommended to use the load driver class to register the driver *@throwsException*/@Test Public voidTest3 ()throwsexception{//Driver Driver = new Com.mysql.jdbc.Driver (); //registers a driver by loading a static block of code by getting a byte-code objectClass.forName ("Com.mysql.jdbc.Driver"); //Driver driver2 = new Com.oracle.jdbc.Driver (); //1. Registering a driver (multiple drivers can be registered)//drivermanager.registerdriver (driver); //Drivermanager.registerdriver (DRIVER2); //2. Connect to a specific databaseConnection conn =drivermanager.getconnection (URL, user, password);            SYSTEM.OUT.PRINTLN (conn); }}

Third, the API of the JDBC interface core

|-Driver Interface: Represents the Java driver interface.                        All the specific database vendors want to implement this interface. |-Connect (URL, properties): A way to connect to a database.                            URL: URL URL syntax for connection database: JDBC Protocol: Database Sub-protocol://HOST: Port/Database User: Username of database Password: Database user password |-DriverManager class: Driver manager class for managing all registered drivers |-registerdriver  (driver): Register the Driver class object |-connection getconnection (Url,user,password);                    Get Connection Object |-Connection interface: A Connection object that represents a Java program and database. |-Statement createstatement (): Creating Statement objects |-PreparedStatement preparestatement (String sql) creating PreparedStatement objects |-callablest Atement preparecall (String sql) Create CallableStatement Object |-statement interface: For executing static SQL statements | |-int EX Ecuteupdate (String sql): Execute static Update SQL statement (DDL,DML) |-ResultSet executeQuery (String sql): Static query SQL statement executed (       DQL) |-preparedstatement interface: Used to execute precompiled SQL statements                 |-int executeupdate (): Perform Precompiled update SQL statement (DDL,DML) |-resultset executeQuery (): Perform pre-compilation Query SQL statement (DQL) |-callablestatement interface: SQL statement for execution of stored procedures (call XXX) |-resultset ex                    Ecutequery (): How to call a stored procedure |-resultset interface: Used to encapsulate the data of a query |-Boolean next (): Move the cursor to the next line |-GETXX (): Gets the value of the column

Getting Started with Jdbc

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.