赞
踩
Statement对象用Connection的方法createStatement创建,代码如下:
- Connection connection = DriverManager.getConnection(url,user,password);
- Statement statement = connection.createStatement();
为了执行Statement对象,被发送到数据库的sql语句将被作为参数提供给Statement方法:
ResultSet resultSet = statement.executeQuery("select * from student");
Statement接口提供了三种执行sql语句的方法:executeQuery、executeUpdate和execute。
executeQuery:
用于产生单个结果集语句,例如select语句。在检索完ResultSet对象的所有行时该语句完成。
executeUpdate:
用于执行insert、update或delete语句以及aqlddl(数据定义语言)语句。当它执行时就完成了。
execute:
用于执行返回多个结果集、多个更新计数或者二者组合的语句。在检索所有结果集或它生成的更新计数之后语句才完成。
Statement对象用于将sql语句发送到数据库中。
实际上有三种Statement对象,它们都作为在连接上执行sql语句的包容器,他们都专用于发送特定类型sql语句:
Statement对象用于执行不带参数的简单sql语句;
PreparedStatement对象用于执行带或不带IN参数的预编译sql语句;
CallableStatement对象用于执行对数据库已存储过程的调用。
Statement接口提供了执行语句和获取结果的基本方法;
PreparedStatement接口添加了处理IN参数的方法;
CallableStatement添加了处理OUT参数的方法。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。