Jdbc-jdbc Common Interface Introduction

Source: Internet
Author: User
Tags stmt

1. What is JDBC?

-1.jdbc (Java database Connection) provides a unified programming interface for Java developers to use a database, which consists of a set of Java classes and interfaces . is the standard API for Java programs to communicate with databases.

The JDBC API allows developers to connect to a database and perform operations in a pure Java manner.

-2.sun company because do not know each mainstream commercial database program code, therefore cannot write code to connect each database, therefore, Sun company itself provides a set of API, as long as the database wants to connect with Java,

the database vendor must implement the JDBC interface itself . and the database vendor JDBC implementation, we call it the driver of this database.

2.JDBC Accessing the database process

  1. Loading the JDBC Driver

2. Establish a connection to the database

3. Sending SQL statements

4. Get SQL Execution Results

3.Java program and database connection, in fact, is the socket connection.

4.JDBC Common Interface

Driver interface

-The driver interface is provided by the database manufacturer, and for Java developers it is only necessary to use the driver interface.

-To connect to a database in programming, you must first load a specific vendor's database driver. Different databases have different loading methods.

-Driver: It is the JDBC interface proposed by each manufacturer to implement the Sun company. That is, the jar file of the implementation class for interfaces such as connection.

-Load MySQL driver: class.forname ("Com.mysql.jdbc.Driver");

-Load Oracle Driver: Class.forName ("Com.jdbc.driver.OracleDriver");

DriverManager interface

-The DriverManager interface is a JDBC management layer that acts between the user and the driver.

-DriverManager tracks the available drivers and establishes a connection between the database and the corresponding driver.

Connection interface

-Connection the connection (session) to a particular database, executes the SQL statement in the connection context, and returns the result.

-DriverManager's Getconnection () method establishes the database connection connection defined in the JDBC URL

-mysql

Connection con = drivermanager.getconnection ("Jdbc:mysql://host:port/database", "User", "password");

-oracle

Connection con = drivermanager.getconnection ("Jdbc:oracle:[email protected]:p ort/database", "User", "password");

Statement interface

-the object used to execute a static SQL statement and return the result it produces.

-Three types of statement:

-Statement:

Created by createstatement for sending simple SQL statements (without parameters)

-PreparedStatement:

-inherits from the statement interface, which has preparedstatement created for sending SQL statements that contain one or more input parameters. PreparedStatement Object

is more efficient than statement objects and is to prevent SQL injection. We generally use PreparedStatement.

-CallableStatement:

-Inherit from PreparedStatement. Created by method Preparecall to invoke a stored procedure.

-Common methods of statement

-Execute (): Runs the statement and returns whether there is a result set.

-ExecuteQuery (): Run the SELECT statement to return to the ResultSet results section

-Executeupdate (): Runs the insert/update/delete operation, returning the number of rows updated.

 Packagecom.yf.jdbc.test;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importjava.sql.Statement;/*** Test execution of SQL statements and SQL injection issues *@authorYANGF **/ Public classDemo02 { Public Static voidMain (string[] args) {Try {            //Load Database driverClass.forName ("Com.mysql.jdbc.Driver"); //get Connection object Setup and database connection            /** The inside of the connection object actually contains the socket object, which is a remote connection. More Time consuming!             This is a key point of connection object management. * Back through the connection pool to do*/Connection con=Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/testjdbc", "root", "123456"); Statement stmt=con.createstatement (); String SQL= "INSERT into T_user (username,pwd,regtime) VALUES (' yyy ', 123123,now ())";                        Stmt.execute (SQL); String SQL1= "Delete from T_user where id = 4";            Stmt.execute (SQL1); //Test SQL injection//because it is an external incoming parameter, I can modify the db arbitrarily, resulting in SQL injection, so we do not apply statementString id = "5 or 1 = 1"; String SQL2= "Delete from T_user where id =" +ID;                    Stmt.execute (SQL2); } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); }    }}
 Packagecom.yf.jdbc.test;Importjava.sql.Connection;Importjava.sql.Date;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;Importjava.sql.SQLException;/*** Test the basic usage of PreparedStatement *@authorYANGF **/ Public classDemo03 { Public Static voidMain (string[] args) {Try {            //Load Database driverClass.forName ("Com.mysql.jdbc.Driver"); //get Connection object Setup and database connectionConnection con =Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/testjdbc", "root", "123456"); String SQL= "INSERT into T_user (username,pwd,regtime) VALUES (?,?,?)"; PreparedStatement PS=con.preparestatement (SQL); Ps.setstring (1, "YANGF"); Ps.setint (2, 888888); Ps.setdate (3,NewDate (System.currenttimemillis ()));        Ps.execute (); } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); }    }}

ResultSet interface

-statement returns the result set when executing the SQL statement.

-ResultSet provides methods for retrieving different types of fields, which are commonly used:

-GetString (): Gets an object in the database that is a data type such as Varchar,char.

-GetFloat (): Get a Float type object in the database

-GetDate (): Get the Date type object in the database

-Getboolean (): Gets the data in the database that is a Boolean type

-Close the objects and connections that are used sequentially

Result->statement->connection

Jdbc-jdbc Common Interface Introduction

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.