当前位置:网站首页>Six steps of JDBC programming
Six steps of JDBC programming
2022-06-21 18:49:00 【LvhaoIT】
JDBC Six steps to programming
List of articles
The overall steps
First step : Registration drive ( tell java, What database are you about to connect to )
The second step : Get the connection ( Express JVM The channel between the database process and the database process is opened , This belongs to the communication between processes Letter , heavyweight , Be sure to turn it off after use )
The third step : Get database operation object ( Special execution sql Object of statement )
Step four : perform SQL sentence (DQL DML …)
Step five : Process query result set ( Only if step four is select At the time of statement , The fifth step is to process the query result set )
Step six : Release resources (java And database belong to the communication between processes , After opening, be sure to close )
First step 、 Registration drive
Code
new How to write it ( Not commonly used )
//1、 Registration drive
Driver driver = new com.mysql.cj.jdbc.Driver(); // polymorphic , A parent type reference points to a child type
// Driver driver = new oracle.jdbc.driver.OracleDriver(); oracle drive
DriverManager.registerDriver(driver);
Reflective writing ( Commonly used )
//1、 Registration drive
Class.forName(com.mysql.cj.jdbc.Driver);
The second step : Get the connection
URL Which parts are included
agreement
ip
PORT Port number
Resource name
http://182.61.22.7:80/index.html
http:// Communication protocol
182.61.22.7 The server ip Address
80 The port of the software on the server
index.html Is the name of a resource on the server
explain :localhost and 127.0.0.1 It's all local ip
What is communication protocol , What's the usage? ?
Communication protocol is the data transmission format set in advance before communication .
How does the data packet transmit data , The format is set in advance
//Oracel url
Oracle JDBC: Oracle:thin:@localhost:1521:orcl
Code
//2、 Get the connection
String url = "jdbc:mysql://127.0.0.1:3306/csdb?serverTimezone=UTC";
String user = "root";
String password = "******";
Connection conn = DriverManager.getConnection(url, user, password);
//[email protected]
System.out.println(" Connection object :" + conn);
The third step : Get database operation object
Code
//3、 Get database operation object
//Startement Special execution sql sentence .
Statement stmt = conn.createStatement();
Step four : perform SQL sentence
Be careful :JDBC Medium sql Statement does not need to provide a semicolon end
//4、 perform sql
//insert
String sql = "insert into dept values(50,' The personnel department ',' Beijing ')";
// Special execution DML Of the statement (insert ,delete ,update)
// The return value is “ Affect the number of records in your database ”
int count = stmt.executeUpdate(sql);
System.out.println(count == 1 ? " Saved successfully " : " Save failed ");
//delete
String sql = "delete from dept where deptno = 50";
int count = stmt.executeUpdate(sql);
System.out.println(count == 1 ? " Delete successful " : " Delete failed ");
//updata
String sql = "update dept set deptno = 60 where deptno = 50 ";
System.out.println(count == 1 ? " The update is successful " : " Update failed ");
System.out.println(count == 1 ? " The update is successful " : " Update failed ");
//create
String sql = "select empno ,ename,sal from emp";
ResultSet rs = stmt.executeQuery(sql);
Step five : Process query result set
5、 Processing result set
rs.next Will move the result set cursor down one , If there is data return at the moved position true, Otherwise return to false
rs.getString, Take out the data , No matter what type , All converted into String type , Which columns of the database are from 1 Start
while (rs.next()) {
// It is best to use the name of the column name of the query result here
int empno = rs.getInt("empno");
String ename = rs.getString("ename");
double sal = rs.getDouble("sal");
System.out.println(empno + ',' + ename + ',' + sal);
}
Traverse rs Schematic diagram

Step six : Release resources
Release resources
In order to ensure the release of resources , stay finally Close the resource in the statement block
And it should be closed from the largest to the largest
The results were analyzed respectively try..catch
try {
if (rs != null) {
rs.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
Use the resource binder to write jdbc Programming
jdbc.properties file
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/csdb?serverTimezone=UTC
user=root
password=lh051920
Code file
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class jdbc Resource binder configuration {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("JDBC Register driver and acquire driver /jdbc");
Connection conn = null;
Statement stmt = null;
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
try {
//1、 Registration drive
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
//[email protected]
System.out.println(" Connection object :" + conn);
//3、 Get database operation object
//Startement Special execution sql sentence .
stmt = conn.createStatement();
//4、 perform sql
String sql = "delete from dept where deptno = 60 or deptno = 70 or deptno = 90";
// Special execution DML Of the statement (insert ,delete ,update)
// The return value is “ Affect the number of records in your database ”
int count = stmt.executeUpdate(sql);
System.out.println(count == 1 ? " Saved successfully " : " Save failed ");
//5、 Processing result set
} catch (Exception throwables) {
throwables.printStackTrace();
} finally {
//6、 Release resources
// In order to ensure the release of resources , stay finally Close the resource in the statement block
// And it should be closed from the largest to the largest
// The results were analyzed respectively try..catch
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
边栏推荐
猜你喜欢
随机推荐
Generics of typescript
9. suffix and prefix functions -suffix basename addsuffix addprefix
互联网通信流程
Full screen menu animation effect expansion in the upper left corner of SVG
Initialization of typescript class objects
C语言__attribute__(packed)属性(学习一下)
【艾思软件】微信小程序开发报价方案模版
Deep Copy
Servlet规范(一)
SVG+Canvas粒子动态效果
Node module management description file
How typescript is constructed
AWS Device Shadow使用
剑指offer 58.对称的二叉树
canvas交互式颜色渐变js特效代码
让接口自动化测试灵动起来
Node输出方式
Compound type of typescript
Disclose the design idea of MVVM framework supporting Baidu search, feed and applet, and San core personnel have made great efforts to build it
写着玩的处理框架








