First step : Register driver ( tell Java program , About to connect to the database of that brand )
Step two : Get connection ( express JVM The channel between the process and the database process is open , This is communication between processes , Heavyweight , Be sure to close it after use )
Step 3 : Get database operation object ( Special execution sql Statement object ) Step 4 : implement SQL sentence (DQL DML ...)
Step 5 : Process query result set ( Only the fourth step is performed select Statement time , To perform step 5 )
Step 6 : Release resources ( Be sure to close the resource after using it .Java And database belong to interprocess communication , Be sure to close it after opening it ) package com.xzh.jdbc; import
javax.xml.transform.Result; import java.sql.*; public class Test { public
static void main(String[] args) { Connection conn = null; Statement stmt =
null; ResultSet res = null; try { // First step : Register driver ( tell Java program , About to connect to the database of that brand )
Class.forName("com.mysql.jdbc.Driver"); //
Step two : Get connection ( express JVM The channel between the process and the database process is open , This is communication between processes , Heavyweight , Be sure to close it after use ) String url =
"jdbc:mysql://localhost:3306/xzh"; String user = "root"; String password =
"333"; conn = DriverManager.getConnection(url, user, password); //
Step 3 : Get database operation object ( Special execution sql Statement object ) stmt = conn.createStatement(); // Step 4 : implement SQL sentence (DQL
DML ...) String sql = "select empno as a,ename from emp"; res =
stmt.executeQuery(sql);// Special execution DQL sentence // Special execution DML Declarative (insert delete update) //
The return value is “ Affect the number of records in the database ” //int c =stmt.executeUpdate(sql); //
Step 5 : Process query result set ( Only the fourth step is performed select Statement time , The fifth step will be carried out ) while (res.next()) { String empno =
res.getString("a"); String ename = res.getString("ename");
System.out.println(empno + "," + ename); } } catch (SQLException e) {
e.printStackTrace(); }catch (ClassNotFoundException e){ e.printStackTrace();
}finally { // Step 6 : Release resources ( Be sure to close the resource after using it .Java And database belong to interprocess communication , Be sure to close it after opening it ) if (stmt !=
null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } }
if (conn != null) { try { conn.close(); } catch (SQLException e) {
e.printStackTrace(); } } } } }

Technology
©2019-2020 Toolsou All rights reserved,
C++ of string of compare usage MySQL Basics Commonly used sentence ( Add / delete / modify query )C Language of a Gobang game implementation QT5.9 Use of learning notes QSqlQuery Method of Qt Getting Started tutorial 【 Basic controls 】QCalendarWidget calendar control java Polymorphic array of web Front end signature plug-in _signature_pad Plug in implements electronic signature function centos7 install RabbitMqspringboot use redis Experiment 4 Automated test tools - software test