当前位置:网站首页>JDBC statement + resultset introduction
JDBC statement + resultset introduction
2022-07-29 04:54:00 【Chen Yuchen】
JDBC-Statement
- Statement The function of is to execute SQL sentence ( It will target different sql Statement executes different methods )
- perform DDL perhaps DML sentence ----》 The corresponding will execute int executeUpdate(String sql) Return to one int Type data , Indicates the affected row
- perform DQL -----》 Corresponding to be executed ResultSet executeQuery(String sql) Return to one ResultSet object
- explain :DDL ( Data definition language ) Common commands are creat drop alter Wait for the order
- DML( Data operation language ) Common commands are insert delete update
- DQL( Data query language )select Return an object
- Among them in DML And DDL There are differences. :DML The return value must be >0, and DDL If the execution is successful, it may return 0. Try to use less in subsequent development DDL command
Case code
/** * perform DML sentence * @throws Exception */
@Test
public void testDML() throws Exception {
//1. Registration drive
//Class.forName("com.mysql.jdbc.Driver");
//2. Get the connection : If the connection is local mysql And the port is the default 3306 Can simplify writing
String url = "jdbc:mysql:///db1?useSSL=false";
String username = "root";
String password = "1234";
Connection conn = DriverManager.getConnection(url, username, password);
//3. Definition sql
String sql = "update account set money = 3000 where id = 1";
//4. Access to perform sql The object of Statement
Statement stmt = conn.createStatement();
//5. perform sql
int count = stmt.executeUpdate(sql);// After execution DML sentence , Rows affected
//6. Processing results
//System.out.println(count);
if(count > 0){
System.out.println(" Modification successful ~");
}else{
System.out.println(" Modification failed ~");
}
//7. Release resources
stmt.close();
conn.close();
}
/** 1. perform DDL sentence 2. @throws Exception */
@Test
public void testDDL() throws Exception {
//1. Registration drive
//Class.forName("com.mysql.jdbc.Driver");
//2. Get the connection : If the connection is local mysql And the port is the default 3306 Can simplify writing
String url = "jdbc:mysql:///db1?useSSL=false";
String username = "root";
String password = "1234";
Connection conn = DriverManager.getConnection(url, username, password);
//3. Definition sql
String sql = "drop database db2";
//4. Access to perform sql The object of Statement
Statement stmt = conn.createStatement();
//5. perform sql
int count = stmt.executeUpdate(sql);// After execution DDL sentence , May be 0
//6. Processing results
System.out.println(count);
//7. Release resources
stmt.close();
conn.close();
}
JDBC-ResultSet
- ResultSet( Result set object ) The role of :** Encapsulates the SQL The result of the query statement **
- ResultSet Object provides methods for manipulating result data
boolean next()
- Move the cursor forward one line from the current position
- Judge whether the current line is a valid line
Method return value description :
- true : It works , There is data in the current row
- false : Invalid line , There is no data in the current row
xxx getXxx( Parameters ): get data
- xxx : data type ; Such as : int getInt( Parameters ) ;String getString( Parameters )
- Parameters
- int Parameters of type : Number of columns , from 1 Start
- String Parameters of type : Column name
Case code
/** * perform DQL * @throws Exception */
@Test
public void testResultSet() throws Exception {
//1. Registration drive
//Class.forName("com.mysql.jdbc.Driver");
//2. Get the connection : If the connection is local mysql And the port is the default 3306 Can simplify writing
String url = "jdbc:mysql:///db1?useSSL=false";
String username = "root";
String password = "1234";
Connection conn = DriverManager.getConnection(url, username, password);
//3. Definition sql
String sql = "select * from account";
//4. obtain statement object
Statement stmt = conn.createStatement();
//5. perform sql
ResultSet rs = stmt.executeQuery(sql);
//6. Processing results , Traverse rs All data in
/* // 6.1 Move the cursor down one line , And judge whether there is data in the current line while (rs.next()){ //6.2 get data getXxx() int id = rs.getInt(1); String name = rs.getString(2); double money = rs.getDouble(3); System.out.println(id); System.out.println(name); System.out.println(money); System.out.println("--------------"); }*/
// 6.1 Move the cursor down one line , And judge whether there is data in the current line
while (rs.next()){
//6.2 get data getXxx()
int id = rs.getInt("id");
String name = rs.getString("name");
double money = rs.getDouble("money");
System.out.println(id);
System.out.println(name);
System.out.println(money);
System.out.println("--------------");
}
//7. Release resources
rs.close();
stmt.close();
conn.close();
}
end !!!!!
边栏推荐
- Wps如何使用智能填充快速填充数据?Wps快速填充数据的方法
- Review key points and data sorting of information metrology in the second semester of 2022 (teacher zhaorongying of Wuhan University)
- [untitled]
- GCC基础知识
- leetcode 763. Partition Labels 划分字母区间(中等)
- Go面向并发的内存模型
- Correct user dragging method
- Configure st-gcn environment record [Google lab]
- Excel卡住了没保存怎么办?Excel还没保存但是卡住了的解决方法
- 安装spinning up教程里与mujoco对应的gym,报错mjpro150
猜你喜欢

UE plays video in scene or UMG

IOS interview preparation - IOS

spinning up安装完使用教程测试是否成功,出现Library“GLU“ not found和‘from pyglet.gl import *错误解决办法

Classes and objects (III)

Flutter 手势监听和画板实现

How is the entered query SQL statement executed?

如何避免示波器电流探头损坏

在线教育的推荐系统

Excel卡住了没保存怎么办?Excel还没保存但是卡住了的解决方法

office2010每次打开都要配置进度怎么解决?
随机推荐
Introduction to auto.js script development
Take you to understand JS array
MySQL time calculation function
EF Core: 一对一,多对多的配置
office2010每次打开都要配置进度怎么解决?
Live in small private enterprises
如何避免示波器电流探头损坏
Numpy basic learning
ssm整合增删改查
带你一文理解JS数组
The difference between the two ways of thread implementation - simple summary
C语言之基础语法
五个关联分析,领略数据分析师一大重要必会处理技能
< El table column> place multiple pictures
spinning up安装完使用教程测试是否成功,出现Library“GLU“ not found和‘from pyglet.gl import *错误解决办法
C language implementation of three chess
Vscode configuration makefile compilation
WPS如何进行快速截屏?WPS快速截屏的方法
How to debug UDP port
def fasterrcnn_ resnet50_ FPN () instance test