Java程序设计——execute()与executeUpdate()(JDBC编程)

编程入门 行业动态 更新时间:2024-10-07 01:24:43

Java<a href=https://www.elefans.com/category/jswz/34/1771020.html style=程序设计——execute()与executeUpdate()(JDBC编程)"/>

Java程序设计——execute()与executeUpdate()(JDBC编程)

目录

一、execute()

二、executeUpdate


一、execute()

execute():几乎可以执行任何SQL语句,返回值是boolean值,表明该SQL语句是否返回ResultSet对象

boolean值:

  • true:使用getResultSet()方法获取返回的ResultSet对象
  • false:使用getUpdateCount()方法获取返回所影响的行数

import java.sql.*;
public class TestDemo {public static void main(String[] args) {JDBCDemo jdbc = new JDBCDemo();}
}class JDBCDemo{public JDBCDemo() {try {//  1.Class.forName("com.mysql.cj.jdbc.Driver");//	2.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/de?characterEncoding=gb2312&serverTimezone=UTC&useSSL=false", "root", "root");//	3.Statement stmt = con.createStatement();//	4.String sql = "SELECT * FROM tb_student";    //  sql语句Boolean bool = stmt.execute(sql); System.out.println("bool = " + bool);if (bool) {ResultSet rs = stmt.getResultSet();		//	获取ResultSet对象//	5.while(rs.next()){int studentNo = rs.getInt(1); // 指定第几列 String studentName = rs.getString("studentName"); // 指定列名称System.out.println(studentNo + "---" + studentName);}//  6.rs.close();stmt.close();con.close();}} catch (SQLException | ClassNotFoundException e) {e.printStackTrace();}}
}


二、executeUpdate

executeUpdate():用于执行DDL和DML语句,返回值为所影响的行数

executeUpdate增强版(Java8新增):executeLargeUpdate(),返回值为long类型,适用于DML语句的记录超过Interger.MAX_VALUES的情况

import java.sql.*;
public class TestDemo {public static void main(String[] args) throws ClassNotFoundException, SQLException {JDBCDemo jdbc = new JDBCDemo();}
}class JDBCDemo{public JDBCDemo() throws SQLException, ClassNotFoundException {//  1.Class.forName("com.mysql.cj.jdbc.Driver");//	2.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/de?characterEncoding=gb2312&serverTimezone=UTC&useSSL=false", "root", "root");//	3.Statement stmt = con.createStatement();//	4.String sql = "UPDATE tb_student SET studentName='狗蛋' WHERE studentNo=2013110101";   //  sql语句int res = stmt.executeUpdate(sql); //	5.System.out.println( "所影响的记录行数:" + res );//  6stmt.close();con.close();}}


更多推荐

Java程序设计——execute()与executeUpdate()(JDBC编程)

本文发布于:2024-02-06 16:00:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1750009.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序设计   Java   execute   JDBC   executeUpdate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!