JDBC 中的execute()、executeQuery() 和executeUpdate() 方法有什么区别?
技术百科
WBOY
发布时间:2023-09-17
浏览: 次 一旦您创建了语句对象,您可以使用Statement接口的execute()、executeUpdate()和executeQuery()方法之一来执行它。
execute()方法:该方法用于执行SQL DDL语句,它返回一个布尔值,指定是否可以检索到ResultSet对象。
示例
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Example {
public static void main(String args[]) throws SQLException {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Creating the Statement
Statement stmt = con.createStatement();
//Executing the statement
String createTable = "CREATE TABLE Employee( "
+ "Name VARCHAR(255), "
+ "Salary INT NOT NULL, "
+ "Location VARCHAR(255))";
boolean bool = stmt.execute(createTable);
System.out.println(bool);
}
}输出
Connection established...... false
executeUpdate(): 这个方法用于执行插入、更新、删除等语句。它返回一个整数值,表示受影响的行数。
示例
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class ExecuteUpdateExample {
public static void main(String args[]) throws SQLException {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Creating the Statement
Statement stmt = con.createStatement();
String insertData = "INSERT INTO Employee("
+ "Name, Salary, Location) VALUES "
+ "('Amit', 30000, 'Hyderabad'), "
+ "('Kalyan', 40000, 'Vishakhapatnam'), "
+ "('Renuka', 50000, 'Delhi'), "
+ "('Archana', 15000, 'Mumbai')";
int
i = stmt.executeUpdate(insertData);
System.out.println("Rows inserted: "+i);
}
}输出
Connection established...... Rows inserted: 4
executeQuery():此方法用于执行返回表格数据的语句(例如 select)。它返回 ResultSet 类的对象。
示例
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ExecuteQueryExample {
public static void main(String args[]) throws SQLException {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Creating the Statement
Statement stmt = con.createStatement();
//Retrieving data
ResultSet rs = stmt.executeQuery("Select *from Employee");
while(rs.next()) {
System.out.print("Name: "+rs.getString("Name")+", ");
System.out.print("Salary: "+rs.getInt("Salary")+", ");
System.out.print("City: "+rs.getString("Location"));
System.out.println();
}
}
}输出
Connection established...... Name: Amit, Salary: 30000, City: Hyderabad Name: Kalyan, Salary: 40000, City: Vishakhapatnam Name: Renuka, Salary: 50000, City: Delhi Name: Archana, Salary: 15000, City: Mumbai
# 您可以
# 对象
# 接口
# select
# mysql
# sql
# 行数
# 布尔值
# connection
# jdbc
# mysqlUrl
# localhost
# registerDriver
# throws
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- Windows音频驱动无声音原因解析_声卡驱动错误
- Mac怎么设置登录项_Mac管理开机自启动程序【教
- Windows怎样拦截QQ浏览器广告_Window
- 如何在Golang中写入XML文件_生成符合规范的
- Mac如何备份到iCloud_Mac桌面与文稿文件
- Dapper的Execute方法的返回值是什么意思
- Win11怎么设置DNS服务器_Windows11
- Mac怎么查看活动监视器_理解Mac进程和资源占用
- Windows10怎么查看系统激活状态_Windo
- php转exe用什么工具打包快_高效打包软件推荐【
- Windows服务无法启动错误1067是什么_进程
- C#怎么使用委托和事件 C# delegate与e
- Win11任务栏怎么放到顶部_Win11修改任务栏
- Win10怎么更改用户名 Win10修改账户名称操
- Windows任务计划服务异常原因_任务调度失败的
- PHP cURL GET请求:正确设置请求头与身份
- 如何使用Golang实现函数指针_函数变量与回调示
- 如何用正则表达式精确匹配“start”到“end”
- Win11怎么更改电脑密码_Windows 11修
- Mac电脑如何恢复出厂设置_Mac抹掉数据并重装系
- XAMPP 启动失败(Apache 突然停止)的终
- C++中的Pimpl idiom是什么,有什么好处
- Win11系统占用空间大怎么办 Win11深度瘦身
- 如何使用正则表达式批量替换重复的 *- 模式为固定
- Win11怎么设置系统还原_Windows11系统
- php打包exe怎么传递参数_命令行参数接收方法【
- Win10系统怎么查看网络连接状态_Windows
- php内存溢出怎么排查_php内存限制调试与优化方
- 如何在Golang中实现服务熔断与限流_Golan
- 如何在Golang中实现基础配置管理功能_Gola
- Win11怎么关闭自动维护 Win11禁用系统自动
- 如何在Golang中捕获HTTP服务器错误_Gol
- Win11如何设置自动关机 Win11定时关机命令
- mac怎么分屏_MAC双屏显示与分屏操作技巧【指南
- 如何使用Golang优化模块引入路径_Golang
- Win10怎么关闭自动更新错误重启 Win10策略
- Win11怎么关闭搜索历史_Win11清除任务栏搜
- c++如何使用std::bitset进行位图算法_
- Win11怎么开启远程桌面_Win11系统远程桌面
- Windows10如何删除恢复分区_Win10 D
- Windows10系统怎么查看硬盘健康_Win10
- Windows 11无法安全删除U盘提示设备正在使
- c++如何用AFL++进行模糊测试 c++ Fuz
- Win10系统怎么查看端口状态_Windows10
- Win11声音忽大忽小怎么办 Win11音频增强功
- 如何在 Python 中将 ISO 8601 时间
- php8.4xdebug无法调试怎么办_php8.
- LINUX怎么设置系统语言_LINUX修改中文环境
- Win11怎么更改输入法顺序_Win11调整语言首
- 如何使用Golang sort排序切片_Golan

i = stmt.executeUpdate(insertData);
System.out.println("Rows inserted: "+i);
}
}
QQ客服