“This article has participated in the call for good writing activities, click to view: the back end, the big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”
1. About JDBC:
Essentially the Api that Java uses to connect to a database,
3 w:
1) I:
Java Database Connectivity: Database Connectivity technology;
Java language database connection technology (other languages also available;)
JDBC: Sun’s public interface for database connection;
(2) how:
All the increase, deletion, change and check of the database are in the use of SQL statements to achieve,
The JDBC interface standard
These so-called SQL Server and MySql, and this ORACLE,
-
1. All database management systems (DBMSS) — manage data by region,
-
2. The operation data is —–>SQL
-
3. Database visualization tools: –>SQL LOy, Navicat, etc
These can be visualized tools, do not have to and CMD doc environment to write command connection, you can instantiate the control, but the implementation is still SQL statements, build tables, you can actually operate;
- 4. To retrieve the data, to use the data, you must connect from the data warehouse, express to fetch, this connection, (in Java, is JDBC).
③ Why does JDBC exist?
Sun worked out the interface, (how to connect, how to prepare for the connection)
Each DBMS, according to their own product to implement classes, to implement connected (driver class)
This driver class, in plain English, is about the Sun-Java interface, but many mobile phone manufacturers, to transfer data, (to the handset manufacturer to implement this interface, through the USB connection).
How to use it is that Java programmers are supposed to program against this common interface,
But to run it, you have to drive the class to connect;
2. JDBC:
These common interfaces in jDBC and the DBMS implementation classes (driver JARS) are declared to enable Java code to connect to the DBMS and manipulate its data
(Java) –>Sql–> a database —
But the task of the connection is to drive the class; Of the underlying implementation;
Package: java. SQL package and extension package javax. SQL package
Common interfaces:
1) Connection: the Connection
②Statement and PreparedStatement:
③ResultSet: accept and process query results — ResultSet
Common helper classes:
①DriverManager: driver management class
3.JDBC connection steps:
Mysql-connection-5.0.8.jar (mysql-connection-5.0.8.jar)
In this case, if you don’t register the driver, compile will not report an error, run time will report an error,
② Connect to database:
Connection interface and DriverManager driver management;
③ Database operation:
—– add/delete: Statement or PrepareStatement
—– query :(results are available) Statement or PrepareStatement
Plus a ResultSet interface;
④ Close the database connection:
Closing resources:
Various closures:
package day_29JDBC.atguigui; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import org.junit.Test; /* * instantiate the JDBC object and get the JDBC connection object; Connect to the database: connect to the database URL --getConnection method; * Point 3: Operation: add or delete the check; * Create a Statement object * then a method of excuteUpate(SQL) * * Close resource: st.close(); * connection.close(); * */ public class JdbcConnection {@test // unit Test junnit Public void connection() throws Exception{/* * Unregistered driver error * java.sqL.sqlexception: No suitable driver found for JDBC: mysql: / / localhost: 3306 / storebook4 * * database name write error: * com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown database 'storebook4' * * mysql exception: * java.sql.sqlexception: Access denied for user 'root'@'localhost' (using password: YES) * * Communications link failure due to underlying exception: * * */ /1 Mysql-connection jar (mysql-connection jar); * Add this JAR to the running class; Build path; * Load this class into memory, (register) * * * * */ // register the driver to load the driver into memory; Class.forName("com.mysql.jdbc.Driver"); /* The Driver is registered with this class * com.mysql.jdbc.driver * */ /2 in the mysql-connection JAR package we added. /* <☆> DriverManager getConnection * ① Connect to url- * URL: uniform resource locator (URL); Name of the DBMS: // Host number: port number/resource path * * * Http://localhost:8080:/ path name (hello. HTML) * * * http://localhost:8080/day05_Tomcat/hello.html DriverManager.getConnection(url, user, password) * * */ String url="jdbc:mysql://localhost:3306/bookStore4"; Connection connection = DriverManager.getConnection(url, "root", "123456"); /* * Connection is an interface created by DriverManager * * */ System.out.println(Connection); / / get the connection object / / com. Mysql. JDBC. Connection @ 4361 bd48 System. Out. The println (connection. The getClass ()); / / get the runtime type/class/com. Mysql.. JDBC Connection / / 3. /* * add the Statement to the database, and then add the object to the upadate. Create the createStatement object for the Statement interface. Import java.sql.Statement. * Statement st=connection.createStatement(); * * * */ Statement st=connection.createStatement(); String SQL ="INSERT INTO lable VALUES(3,'微博')"; int count=st.executeUpdate(sql); // To verify success, the executeUpdate(SQL) method returns an integer. Println (count>0? Add succeeded ":" add failed "); . / * * * com. Mysql. JDBC Connection @ 36 aa7bc2 * class com in mysql. JDBC. Connection * add success * * * / / / 4. Close connection: st.close(); // Close the object connection.close(); // Close the connection}}Copy the code