Introduction to JDBC:
JDBC is all called Java Database Connectivity, and is the abbreviation for Sun's specified Java connection technology.
He is a DBMS-independent application interface developed jointly by Sun and database developers, which provides a unified API for Java programmers to program database programming.
JDBC actually has two sets of APIs, one for Java application developers, the other for database driver developers, and generally for data driver developers to exploit the APIs used by the provider developers using the next set of APIs.
We need to be familiar with the classes (class), interfaces (interface), exceptions (expection) defined in the package java.sql.*, and have the flexibility to use them to be able to play JDBC's powerful functions.
a few core classes and interfaces in package java.sql.*:
①java.sql.drivermanager is used to load JDBC drivers for different database vendors and provides support for creating new database connections
②java.sql.driver The driver entry for the specified database, DriverManager will use this class as the parameter for the connection data
③java.sql.connection completing the connection function for a specified database
④java.sql.statement in an already established connection, as a container for SQL statement execution, it has two subclasses:
Java.sql.CallableStatement is used to execute stored procedures that have been created in the database.
Java.sql.preparedStatement is used to execute precompiled SQL statements.
The ⑤java.sql.resultset is used to store the result set returned after executing a specific SQL statement.
The basic process for querying a JDBC connection to a database table is as follows:
①class.forname ("Com.mysql.jdbc.Driver"). newinstance ();
Function: Load the driver class, here load the MySQL JDBC driver
②connection conn = drivermanager.getconnection (URL, "user", "password");
Role: Establish a connection
③statement Statement = Conn.createstatement ();
Function: A container that is executed by connecting SQL statements through this connection
④string sql = "SELECT * FROM table1";
Role: Creating SQL statements
⑤resultset rs = statement.executequery (SQL);
Function: Executes the SQL statement just created using the preceding SQL container and returns the structure to the result set object.
Query results obtained by ⑥ analysis
A simple example is as follows:
1 Public classJdbctest {2 Public StaticConnection getconnection ()throwsSQLException,3 java.lang.ClassNotFoundException4 {5 //first step: Load MySQL's JDBC driver6 class.forname ("Com.mysql.jdbc.Driver");7 8 //get the URL of the connection, can access the MySQL database user name, password; JSJ: Database name9String url = "Jdbc:mysql://localhost:3306/fuck";TenString username = "root"; OneString password = "861113"; A - //Step Two: Create an instance of the connection class with the MySQL database - Connection con = drivermanager.getconnection (URL, username, password); the returncon; - } - - + Public Static voidMain (String args[]) { - Try + { A //Step Three: Get the connection class instance con, create an instance of the statement object class with Con sql_statement at Connection con = getconnection (); - Statement sql_statement = Con.createstatement (); - - //if a database with the same name exists, delete - //sql_statement.executeupdate ("drop table if exists student"); - //An SQL statement was executed to generate a table named student in //sql_statement.executeupdate ("CREATE TABLE student (ID int not NULL auto_increment, name varchar (a) NOT null default ') Name ', math int not NULL default, primary key (ID)); "); - to //inserting data into the person table + sql_statement.executeupdate ("Insert person values (3, ' liying ', 98)"); sql_statement.executeupdate ("Insert person values (4, ' Jiangshan ',") "); sql_statement.executeupdate ("Insert person values (5, ' Wangjiawu ',") "); sql_statement.executeupdate ("Insert person values (6, ' Duchangfeng ',") "); $Panax Notoginseng //Fourth Step: Execute the query, with the object of the ResultSet class, return the results of the query - String query = "SELECT * from person"; the ResultSet result = sql_statement.executequery (query); + A //Display the contents of the person table in the data: theSYSTEM.OUT.PRINTLN ("The data in the person table is as follows:"); +System.out.println ("------------------------"); -System.out.println ("ordinal" + "+" name "+" "+" score "); $System.out.println ("------------------------"); $ - //processes the resulting query results and operates on the object of the result class - while(Result.next ()) the { - intNumber = Result.getint ("number");WuyiString name = result.getstring ("name"); theString Mathsorce = result.getstring ("Mathsorce"); - Wu //get data from the database -SYSTEM.OUT.PRINTLN ("+ number +" "+ name +" "+mathsorce); About } $ - //closing connections and claims - sql_statement.close (); - con.close (); A +}Catch(java.lang.ClassNotFoundException e) { theSystem.err.print ("ClassNotFoundException"); - System.err.println (E.getmessage ()); $}Catch(SQLException ex) { theSystem.err.println ("SQLException:" +ex.getmessage ()); the } the } the -}
Introduction to JDBC