如果您想了解方法executeQuery和不能在PreparedStatement或CallableStatement上接受参数。错误的知识,那么本篇文章将是您的不二之选。我们将深入剖析方法execu
如果您想了解方法executeQuery和不能在PreparedStatement或CallableStatement上接受参数。错误的知识,那么本篇文章将是您的不二之选。我们将深入剖析方法executeQuery的各个方面,并为您解答不能在PreparedStatement或CallableStatement上接受参数。错误的疑在这篇文章中,我们将为您介绍方法executeQuery的相关知识,同时也会详细的解释不能在PreparedStatement或CallableStatement上接受参数。错误的运用方法,并给出实际的案例分析,希望能帮助到您!
本文目录一览:- 方法executeQuery()不能在PreparedStatement或CallableStatement上接受参数。错误(方法不能被当前类的子类重新定义)
- CallableStatement + registerOutParameter +多行结果
- Java PreparedStatement抱怨execute()上的SQL语法
- java – getGeneratedKeys()在PreparedStatement.executeBatch()之后
- java.sql.PreparedStatement不能转换为com.mysql.jdbc.PreparedStatement
方法executeQuery()不能在PreparedStatement或CallableStatement上接受参数。错误(方法不能被当前类的子类重新定义)
尝试从数据库连接和检索数据时出现此类错误。
方法executeQuery()不能在PreparedStatement或CallableStatement上接受参数。
我的代码是这样的。
String search = request.getParameter("searchstudent");out.println(search);String connectionURL = "jdbc:sqlserver://localhost:1433;databaseName=Chingdb; integratedSecurity=true;"; Connection connection = null; PreparedStatement pstatement = null; Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); ResultSet rs = null;int updateQuery = 0;if(request.getParameter("editstudent")!= null){ try { connection = DriverManager.getConnection(connectionURL, "root", "root"); String queryString = "SELECT P_ID, lname, fname, mname FROM stu_info Where lname = ?"; pstatement = connection.prepareStatement(queryString); pstatement.setString(1, search); rs = pstatement.executeQuery(queryString); updateQuery = pstatement.executeUpdate(); %> <TABLE cellpadding="15" border="1"> <% while (rs.next()) { %> <TR> <TD><%=rs.getInt(1)%></TD> <TD><%=rs.getString(2)%></TD> <TD><%=rs.getString(3)%></TD> <TD><%=rs.getString(4)%></TD> </TR></TABLE> <% rs.close(); pstatement.close(); connection.close(); } } catch(Exception e){ out.println(e); } }
答案1
小编典典您不需要第二次使用queryString,因为您可以用以下方式“告诉” prepareStatement字符串:
pstatement = connection.prepareStatement(queryString);
这是正确的方法:
pstatement = connection.prepareStatement(queryString); pstatement.setString(1, search); rs = pstatement.executeQuery();
CallableStatement + registerOutParameter +多行结果
我有以下形式的SQL语句:
BEGIN\n
UPDATE tab
SET stuff
WHERE stuff
RETURNING intA,intB,stringC
INTO ?,?,?
我已经注册了适当的Out参数。
这是我的一些问题:我叫stmt.executeQuery()还是stmt.execute()?此外,我知道通过普通的SELECT查询,我可以遍历resultSet并填充我的对象-
Out参数的多行等效于什么?
编辑:也许我可以注册CURSOR类型的单输出参数并遍历此结果。
EDIT2:是否可能需要循环使用多个resultSet?谢谢!
Java PreparedStatement抱怨execute()上的SQL语法
这真让我发疯……我在这里做错了什么?
ArrayList<String> toAdd = new ArrayList<String>();toAdd.add("password");try{ PreparedStatement pStmt = conn.prepareStatement("ALTER TABLE testTable ADD ? varchar(100)"); for (String s : toAdd) { pStmt.setString(1, s); pStmt.execute(); }} catch (SQLException e) { e.printStackTrace();}
结果是…
02:59:12,885错误[STDERR]
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的“
password” varchar(100)”附近使用
但…
ArrayList<String> toAdd = new ArrayList<String>();toAdd.add("password");try{ Statement stmt = conn.prepareStatement(); for (String s : toAdd) { stmt.execute("ALTER TABLE testTable ADD "+s+" varchar(100)"); }} catch (SQLException e) { e.printStackTrace();}
完美运行…直接在MySQL命令行客户端直接输入文本也是如此。
mysql> alter table testTable add stringGoesHere varchar(100);Query OK, 1 row affected (0.23 sec)Records: 1 Duplicates: 0 Warnings: 0
我究竟做错了什么?
答案1
小编典典在MySQL手册明确表示,?
(参数标记)是唯一,不列名绑定的数据值。
参数标记只能用于应显示数据值的地方,而不能用于SQL关键字,标识符等。
因此,您将不得不使用第二种方法。
java – getGeneratedKeys()在PreparedStatement.executeBatch()之后
ps = con.prepareStatement(query,PreparedStatement.RETURN_GENERATED_KEYS); for(Element e:listofElements){ ps.setString(1,this.col_val_1); ps.setString(2,this.col_val_2); ps.setInt(3,this.col_val_3); ps.addBatch(); } ps.executeBatch(); ResultSet rs = ps.getGeneratedKeys();
在这一点上,我希望得到每个INSERT生成的PK,我得到这个sqlServerException:
com.microsoft.sqlserver.jdbc.sqlServerException: The statement must be executed before any results can be obtained.
我期望得到一个ResultSet,每一个插入执行一行,所以我可以得到每个PK生成.
我期待错了吗?我做错了吗?可以用不同的方法使用批处理执行吗?
解决方法
我试图在Microsoft网站上寻找一个明确的陈述,但找不到.这个旧的(2007)MSDN上的论坛帖子表示不支持:http://social.msdn.microsoft.com/Forums/en-US/sqldataaccess/thread/6cbf5eea-e5b9-4519-8e86-f4b65ce3f8e1
java.sql.PreparedStatement不能转换为com.mysql.jdbc.PreparedStatement
您的导入存在问题,您必须从java.sql
而非Connection
导入PreparedStatement
,ResultSet
和com.mysql.jdbc
,因此请使用:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
代替此:
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
,
在Connections.getConnection()处,应使用DriverManager初始化连接。
类似
ret.reg_dt = pd.to_datetime(ret.reg_dt,format='%Y-%m-%d %H:%M:%S')
https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html#db_connection_url
今天的关于方法executeQuery和不能在PreparedStatement或CallableStatement上接受参数。错误的分享已经结束,谢谢您的关注,如果想了解更多关于CallableStatement + registerOutParameter +多行结果、Java PreparedStatement抱怨execute()上的SQL语法、java – getGeneratedKeys()在PreparedStatement.executeBatch()之后、java.sql.PreparedStatement不能转换为com.mysql.jdbc.PreparedStatement的相关知识,请在本站进行查询。
本文标签: