JDBC Statement接口实现的execute方法
来源:互联网 发布:背景音乐提取软件 编辑:程序博客网 时间:2024/05/06 16:46
完整方法名为 boolean execute(String sql) throws SQLException;可见其返回值是Boolean类型的,那么什么时候返回的是true,什么时候返回的是false呢?首先我们知道boolean execute 允许执行查询语句、更新语句、DDL语句。返回值为true时,表示执行的是查询语句,可以通过getResultSet方法获取结果;返回值为false时,执行的是更新语句或DDL语句。下面拿一段源码举例:
package javademo;
import java.sql.*;
public class ConnectionDemo01 {
private static final String driver = "org.gjt.mm.mysql.Driver";
private static final String url = "jdbc:mysql://localhost:3306/world?"
+ "characterEncoding=utf8&useSSL=false"; //防止在高版本MySQL上出现警告
private static final String user = "你的用户名";
private static final String password = "你的密码";
public static void main(String[] args) throws Exception {
Connection con = null;
Statement sta = null;
String sql="SELECT * FROM tdb_goods WHERE goods_id=22";
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
sta=con.createStatement();
boolean a=sta.execute(sql);
System.out.println(a);
sta.close();
con.close();
}
}
输出:true,因为执行的是查询功能。
public class ConnectionDemo01 {
private static final String driver = "org.gjt.mm.mysql.Driver";
private static final String url = "jdbc:mysql://localhost:3306/world?"
+ "characterEncoding=utf8&useSSL=false"; //防止在高版本MySQL上出现警告
private static final String user = "你的用户名";
private static final String password = "你的密码";
public static void main(String[] args) throws Exception {
Connection con = null;
Statement sta = null;
String sql="UPDATE tdb_goods SET goods_id=21 WHERE goods_id=22";
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
sta=con.createStatement();
boolean a=sta.execute(sql);
System.out.println(a);
sta.close();
con.close();
}
}
输出:false,因为执行的是更新功能。
package javademo;
import java.sql.*;
public class ConnectionDemo01 {
private static final String driver = "org.gjt.mm.mysql.Driver";
private static final String url = "jdbc:mysql://localhost:3306/world?"
+ "characterEncoding=utf8&useSSL=false"; //防止在高版本MySQL上出现警告
private static final String user = "你的用户名";
private static final String password = "你的密码";
public static void main(String[] args) throws Exception {
Connection con = null;
Statement sta = null;
String sql="SELECT * FROM tdb_goods WHERE goods_id=22";
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
sta=con.createStatement();
boolean a=sta.execute(sql);
System.out.println(a);
sta.close();
con.close();
}
}
输出:true,因为执行的是查询功能。
public class ConnectionDemo01 {
private static final String driver = "org.gjt.mm.mysql.Driver";
private static final String url = "jdbc:mysql://localhost:3306/world?"
+ "characterEncoding=utf8&useSSL=false"; //防止在高版本MySQL上出现警告
private static final String user = "你的用户名";
private static final String password = "你的密码";
public static void main(String[] args) throws Exception {
Connection con = null;
Statement sta = null;
String sql="UPDATE tdb_goods SET goods_id=21 WHERE goods_id=22";
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
sta=con.createStatement();
boolean a=sta.execute(sql);
System.out.println(a);
sta.close();
con.close();
}
}
输出:false,因为执行的是更新功能。
0 0
- JDBC Statement接口实现的execute方法
- JDBC中Statement接口提供的execute、executeQuery和executeUpdate之间的区别
- JDBC中Statement接口提供的execute、executeQuery和executeUpdate之间的区别
- JDBC中Statement接口提供的execute、executeQuery和executeUpdate之间的区别
- JDBC中Statement接口提供的execute、executeQuery和executeUpdate之间的区别(转)
- JDBC中Statement接口提供的execute、executeQuery和executeUpdate之间的区别
- JDBC中Statement接口提供的execute、executeQuery和executeUpdate之间的区别
- JDBC中Statement接口提供的execute、executeQuery和executeUpdate之间的区别
- JDBC中Statement接口提供的execute、executeQuery和executeUpdate之间的区别
- JDBC中Statement 接口提供了三种执行 SQL 语句的方法: executeQuery executeUpdate execute 使用哪一个方法由 SQL 语句所产生的内容决定。
- jdbc中Statement接口提供的execute、executeQuery和executeUpdate三者区别
- JDBC访问数据中Statement接口的常用方法
- JDBC中Statement接口提供的executeUpdate方法
- JDBC--使用statement接口实现更新数据
- JDBC--使用statement接口实现删除数据
- statement接口中executeUpdate()、executeQuery()、execute()方法处理
- statement接口中executeUpdate()、executeQuery()、execute()方法处理
- JDBCTM中Statement接口提供的execute、executeQuery和executeUpdate之间的区别 Statement 接口提供了三种执行 SQL 语句的方法:
- 根据radio单选控制开始结束时间
- error: variable missing(use:--variable API_KEY=value)问题的解决
- c++ 入门知识
- 自定义View放大缩小
- oracle数据库
- JDBC Statement接口实现的execute方法
- jquery.cookie中的操作
- HDU----1083
- ASP.NET服务器端执行脚本的通用方法
- 【华为 OJ 】计算日期到天数的转换
- Java之接口
- NASA 的 10 大编程规则
- Oracle 游标操作
- JAVA的反射机制和动态代理