JDBC 中的execute()、executeQuery() 和executeUpdate() 方法有什么区别?
一旦您创建了语句对象,您可以使用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); } }登录后复制