A. Recall
Remember the previous self-study, just contact the database, the most basic theoretical knowledge and methods of connecting database, now review the basics and summarize as follows!
1.JDBC basic process for connecting to a database
Download the JDBC driver , Url,root,password, connection database for JDBC Connection, Get statement related classes close JDBC related objects by completing the add-and- revise
2. Detailed ①JDBC driver loading
There are 4 ways to talk about JDBC loading drivers, but it's not quite clear. This uses the Java reflection to complete the driver loading. But only if the appropriate jar package is required to load successfully
MAVEN Configuration Jar Package:
<!--mysql driver- <dependency> <groupId>mysql</groupId> <artifactId> mysql-connector-java</artifactid> <version>5.1.9</version> <scope>runtime</ Scope> </dependency>
MySQL Driver: com.mysql.jdbc.Driver
Oracle Driver: Oracle.jdbc.driver.OracleDriver
For example:
Class.forName (com.mysql.jdbc.Driver);//Initialize the JDBC driver and load the appropriate driver into the JVM
② Connection Database
The DriverManager class provided by the Java SE API is used here to get the connection interface of the connection
MySQL url:jdbc:mysql://localhost:3306/test (database name)
Oracle's Url:jdbc:oracle:thin: @localhost: 1521:test (database name)
For example:
String url = "Jdbc:mysql://localhost:3306/test"= "root" = "root" = Drivermanager.getconnection (URL,USER,PASSWD);
③ acquisition of statement related classes for additional pruning
Interface Statement(can only be used to perform static code + stitching):
Method: Boolean Execute (String sql);
int executeupdate (String sql);//executable or delete
ResultSet executeQuery (String sql);//execute query function
Statement Statement = connection.createstatement ();
= Statement.executequery ("SELECT * FROM table1");//The query uses the ExecuteQuery method to return resultset while( Resultset.next ()) { System.out.print (Resultset.getobject (1) + "---" +resultset.getobject (2));
System.out.print (resultset.getstring (1) + + "---" +resultset.getdate (2));}
Interface preparedstatement(dynamic code can be executed):
Method: Boolean execute ();
int executeupdate ();//executable or delete
ResultSet executeQuery ();//execute query function
String sql = "Update test set name =?" WHERE id =? " = connection.preparestatement (sql); ps.setstring (1, "SSX"); Ps.setint (2,1); Ps.execute ();
In addition, interface resultsetmetadate(using the Getmetadate () method) can be obtained by resultset
Method: String getcatalogname (int column) gets the table name
int getColumnCount (): Gets the number of columns of the result set
String getcolumnlabel (int column): Gets the column name of the corresponding column
3. Encapsulating JDBC
You can see here: it's very well written.
"JDBC" connects the database from shallow into deep