Statement、PreparedStatement和CallableStatement

走你_ 2016-12-23 10:10:11
问题1:我用Statement。executeQuery可以执行INSERT 和UPDATE 那executeUPDATE这函数有啥用?


Statement、PreparedStatement这俩可以执行存储过程 也可以得到返回结果集,那CallableStatement 有啥用 ?

求解 最好给个例子 比如 CallableStatement可以做到一些事 Statement、PreparedStatement做不到
executeUPDATE可以做到 executeQuery做不到!



...全文
108 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
soton_dolphin 2016-12-23
  • 打赏
  • 举报
回复
ResultSet executeQuery(String sql) throws SQLException Executes the given SQL statement, which returns a single ResultSet object. Note:This method cannot be called on a PreparedStatement or CallableStatement. Parameters: sql - an SQL statement to be sent to the database, typically a static SQL SELECT statement
soton_dolphin 2016-12-23
  • 打赏
  • 举报
回复
引用 2 楼 beyond789654 的回复:
[quote=引用 1 楼 soton_dolphin 的回复:] 1 首先 Statement 类里,executeQuery 返回一个ResultSet 类,只能执行select 操作。 2 executUpdate返回有四个重写版本,返回 int 基本类型 3, PrepareStatement 支持在SQL 语句里面传入参数,比直接传入字符串sql 命令安全。而且PrepareStatement 里面的sql 命令会提前编译,重复利用,提高效率 4. Callable Statement 更好的支持调用存储过程
只能执行select 操作 我刚刚还试来着 可以执行INSERT UPDATE呀[/quote] 麻烦你仔细看看API。
走你_ 2016-12-23
  • 打赏
  • 举报
回复
引用 1 楼 soton_dolphin 的回复:
1 首先 Statement 类里,executeQuery 返回一个ResultSet 类,只能执行select 操作。 2 executUpdate返回有四个重写版本,返回 int 基本类型 3, PrepareStatement 支持在SQL 语句里面传入参数,比直接传入字符串sql 命令安全。而且PrepareStatement 里面的sql 命令会提前编译,重复利用,提高效率 4. Callable Statement 更好的支持调用存储过程
只能执行select 操作 我刚刚还试来着 可以执行INSERT UPDATE呀
soton_dolphin 2016-12-23
  • 打赏
  • 举报
回复
1 首先 Statement 类里,executeQuery 返回一个ResultSet 类,只能执行select 操作。 2 executUpdate返回有四个重写版本,返回 int 基本类型 3, PrepareStatement 支持在SQL 语句里面传入参数,比直接传入字符串sql 命令安全。而且PrepareStatement 里面的sql 命令会提前编译,重复利用,提高效率 4. Callable Statement 更好的支持调用存储过程
爱睡觉的阿狸 2016-12-23
  • 打赏
  • 举报
回复
返回结果类型不一样,用于不同的判断。
// JBuilder API Decompiler stub source generated from class file // 2010-1-15 // -- implementation of methods is not available package com.ibatis.common.jdbc; // Imports import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.SQLWarning; import java.sql.Savepoint; import java.sql.Statement; import java.util.Map; public class SimplePooledConnection implements InvocationHandler { // Fields private static final String CLOSE = "close"; private static final Class[] IFACES; private int hashCode; private SimpleDataSource dataSource; private Connection realConnection; private Connection proxyConnection; private long checkoutTimestamp; private long createdTimestamp; private long lastUsedTimestamp; private int connectionTypeCode; private boolean valid; // Constructors public SimplePooledConnection(Connection connection, SimpleDataSource dataSource) { } // Methods public void invalidate() { } public boolean isValid() { return false;} public Connection getRealConnection() { return null;} public Connection getProxyConnection() { return null;} public int getRealHashCode() { return 0;} public int getConnectionTypeCode() { return 0;} public void setConnectionTypeCode(int connectionTypeCode) { } public long getCreatedTimestamp() { return 0L;} public void setCreatedTimestamp(long createdTimestamp) { } public long getLastUsedTimestamp() { return 0L;} public void setLastUsedTimestamp(long lastUsedTimestamp) { } public long getTimeElapsedSinceLastUse() { return 0L;} public long getAge() { return 0L;} public long getCheckoutTimestamp() { return 0L;} public void setCheckoutTimestamp(long timestamp) { } public long getCheckoutTime() { return 0L;} private Connection getValidConnection() { return null;} public int hashCode() { return 0;} public boolean equals(Object obj) { return false;} public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null;} public Statement createStatement() throws SQLException { return null;} public PreparedStatement prepareStatement(String sql) throws SQLException { return null;} public CallableStatement prepareCall(String sql) throws SQLException { return null;} public String nativeSQL(String sql) throws SQLException { return null;} public void setAutoCommit(boolean autoCommit) throws SQLException { } public boolean getAutoCommit() throws SQLException { return false;} public void commit() throws SQLException { } public void rollback() throws SQLException { } public void close() throws SQLException { } public boolean isClosed() throws SQLException { return false;} public DatabaseMetaData getMetaData() throws SQLException { return null;} public void setReadOnly(boolean readOnly) throws SQLException { } public boolean isReadOnly() throws SQLException { return false;} public void setCatalog(String catalog) throws SQLException { } public String getCatalog() throws SQLException { return null;} public void setTransactionIsolation(int level) throws SQLException { } public int getTransactionIsolation() throws SQLException { return 0;} public SQLWarning getWarnings() throws SQLException { return null;} public void clearWarnings() throws SQLException { } public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return null;} public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return null;} public Map getTypeMap() throws SQLException { return null;} public void setTypeMap(Map map) throws SQLException { } public void setHoldability(int holdability) throws SQLException { } public int getHoldability() throws SQLException { return 0;} public Savepoint setSavepoint() throws SQLException { return null;} public Savepoint setSavepoint(String name) throws SQLException { return null;} public void rollback(Savepoint savepoint) throws SQLException { } public void releaseSavepoint(Savepoint savepoint) throws SQLException { } public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return null;} public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { return null;} public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { return null;} }

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧