The spring framework is a one-stop framework that encapsulates many technologies and frameworks, making them easier to apply.
Code Procedures for JDBC
/step 1. Import Required PackagesImportJava.sql.*; Public classSelectrecords {//JDBC driver name and database URL Static FinalString jdbc_driver = "Com.mysql.jdbc.Driver"; Static FinalString Db_url = "jdbc:mysql://localhost/jdbc_db"; //Database Credentials Static FinalString USER = "root"; Static FinalString PASS = "123456"; Public Static voidMain (string[] args) {Connection conn=NULL; Statement stmt=NULL; Try{ //STEP 2:register JDBC driverClass.forName ("Com.mysql.jdbc.Driver"); //STEP 3:open a connectionSYSTEM.OUT.PRINTLN ("Connecting to a selected database ..."); Conn=drivermanager.getconnection (Db_url, USER, PASS); System.out.println ("Connected Database successfully ..."); //STEP 4:execute a querySYSTEM.OUT.PRINTLN ("Creating statement ..."); stmt=conn.createstatement (); String SQL= "SELECT ID, First, last, age from student"; ResultSet RS=stmt.executequery (SQL); //STEP 5:extract data from result set while(Rs.next ()) {//Retrieve by column name intid = rs.getint ("id"); intAge = Rs.getint ("Age"); String First= Rs.getstring ("First"); String Last= Rs.getstring ("Last"); //Display ValuesSystem.out.print ("ID:" +ID); System.out.print (", Age:" +Age ); System.out.print (", First:" +First ); System.out.println (", Last:" +Last ); } rs.close (); }Catch(SQLException se) {//Handle errors for JDBCSe.printstacktrace (); }Catch(Exception e) {//Handle errors for Class.forNameE.printstacktrace (); }finally{ //finally block used to close resources Try{ if(stmt!=NULL) Conn.close (); }Catch(SQLException se) {}// do nothing Try{ if(conn!=NULL) Conn.close (); }Catch(SQLException se) {se.printstacktrace (); }//End finally try}//End TrySystem.out.println ("goodbye!");}//End Main}//End Jdbcexample
JDBC Query Database
1. Registering Database Information
2. Get a database connection
3. Execute SQL query
4. Close the database connection
JdbcTemplate Code Procedures
JdbcTemplate completed the section to get the database connection and close the connection, you need to take care of the SQL business implementation later.
1. Import the package (including the corresponding database driver package)
2. Creating objects, setting up database information
3. Create a JdbcTemplate object, set the data source
4. Invoke the method of the JdbcTemplate object to implement the operation
JdbcTemplate Main methods of delivery
Execute method: Can be used to execute any SQL statement, generally used to execute DDL statements;
Update method and BatchUpdate method: Update method is used to execute new, modify, delete and other statements;
The BatchUpdate method is used to execute batch-related statements;
Query method and Queryforxxx method: used to execute queries related to the statement;
Call method: Used to execute stored procedures, function-related statements.
Framework application: Spring FRAMEWORK-JDBC Support