JDBC Statement接口实现的execute方法

完整方法名为 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,因为执行的是更新功能。
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值