kac*_*cpr 0 java sql jdbc callable-statement
我以为这将是一个简单的谷歌搜索,但我找不到答案:
我正在将 Access 数据库移至 SQL Server,我已将一些查询转换为视图。我的 Java 应用程序过去使用简单的方式调用每个查询:
{ call dbo.GetOtherRanges() } //Well, not entirely true - Access did not use 'dbo' schema in front of the name
Run Code Online (Sandbox Code Playgroud)
这适用于存储过程。但是当我对视图执行此操作时,我收到以下信息:
[SQL Server]The request for procedure 'GetOtherRanges' failed because 'GetOtherRanges' is a view object.
Run Code Online (Sandbox Code Playgroud)
我认为这就像删除()视图名称后面的括号一样简单,但这并没有达到目的。
Views可以像在 JDBC 中一样访问Tables。
你不需要CallableStatement为此。
只需使用StatementorPreparedStatement就足够了。
如果您需要帮助,请参阅以下示例代码: 使用视图名称而不是 Table1
public void connectToAndQueryDatabase(String username, String password) {
Connection con = DriverManager.getConnection(
"jdbc:myDriver:myDatabase",
username,
password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) {
int x = rs.getInt("a");
String s = rs.getString("b");
float f = rs.getFloat("c");
}
}
Run Code Online (Sandbox Code Playgroud)