当前位置:网站首页>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 !!!!!
边栏推荐
- Torch.nn.crossentropyloss() details
- 带你一文理解JS数组
- [QT learning notes] * insert pictures in the window
- SGuard64.exe ACE-Guard Client EXE:造成磁盘经常读写,游戏卡顿,及解决方案
- 在线教育的推荐系统
- Improve the readability of your regular expressions a hundred times
- Wechat picture identification
- Five correlation analysis, one of the most important skills of data analysts
- Excel卡住了没保存怎么办?Excel还没保存但是卡住了的解决方法
- 1 sentence of code, get asp Net core binds multiple sources to the same class
猜你喜欢

Climbing the pit of traffic flow prediction (III): using pytorch to realize LSTM to predict traffic flow

1 sentence of code, get asp Net core binds multiple sources to the same class

Correct user dragging method

Use jupyter (2) to establish shortcuts to open jupyter and common shortcut keys of jupyter

WPS插入超链接无法打开,提示“无法打开指定文件”怎么办!

SSM integration, addition, deletion, modification and query

IDEA中使用注解Test

Review key points and data sorting of information metrology in the second semester of 2022 (teacher zhaorongying of Wuhan University)

新产品上市最全推广方案

Torch.nn.crossentropyloss() details
随机推荐
def fasterrcnn_resnet50_fpn()实例测试
1 sentence of code, get asp Net core binds multiple sources to the same class
常见的限流方式
A little knowledge about management
The most comprehensive promotion plan for the launch of new products
PHP determines whether the user has logged in. If logged in, the home page will be displayed. If not, enter the login page or registration page
GCC基础知识
Several simple and difficult OJ problems with sequential force deduction
DataSourceClosedException: dataSource already closed at Mon Oct 25 16:55:48 CST 2021
[QT learning notes] * insert pictures in the window
office2010每次打开都要配置进度怎么解决?
C语言之基础语法
Common rules of makefile (make) (II)
un7.28:redis客户端常用命令。
怎样监测微型的网站服务
盒子水平垂直居中布局(总结)
Implementation of flutter gesture monitoring and Sketchpad
Command line interactive tools (latest version) inquirer practical tutorial
MySQL定时调用预置函数完成数据更新
Reveal安装配置调试