我创建了一个获取nvarchar的过程,并使用like返回一个表,它运行良好。但是当我想在java中使用这个过程时它不起作用。 这是java代码。
String query = "exec Predict ?";
pst = conn.prepareStatement(query);
pst.setString(1, "%"+"ee"+"%");
rs = pst.executeQuery();
int i = 0;
while (rs.next()) {
System.out.println(rs.getString(1));
i++;
}
答案 0 :(得分:0)
嘿,你实际上不能把它称为查询。你必须使它成为一个可调用的语句并执行它。这是我一直在使用的代码。它也接受参数,但如果你不需要它,只需删除它。这是为我的项目设置定制的,但这将帮助你:
/**
* This method takes in a stored procedure along with its parameters and parameter type and creates a result set from them.
* @param storedProcedure
* @param parameters
* @param parameterType
* @return
* @throws SQLException
*/
public static ResultSet createRSFromStoredProc(String storedProcedure, ArrayList<String> parameters, ArrayList<String> parameterType) throws SQLException {
// Initializes variables that are used in the method.
Connection dbConnection = null;
CallableStatement callableStatement = null;
ResultSet rs = null;
// Builds the stored procedure calling
String insertStoreProc = "{call " + storedProcedure + "(";
for(int i=0;i<parameters.size(); i++)
{
if(i==parameters.size()-1)
insertStoreProc+="?";
else
insertStoreProc+="?,";
}
insertStoreProc+=")}";
try {
// establishes the connection
dbConnection = getDBConnection();
// creates the callable statement
callableStatement = dbConnection.prepareCall(insertStoreProc);
// Parses the string into the correct parameter type and sets the parameter to the value.
for(int i=0;i<parameters.size(); i++)
{
if(parameterType.get(i).equals("string"))
callableStatement.setString(i+1, parameters.get(i));
else if(parameterType.get(i).equals("int"))
callableStatement.setInt(i+1, Integer.parseInt(parameters.get(i)));
if(parameterType.get(i).equals("dateTime"))
{
// if now is entered for the time gets the current timestamp
if(parameters.get(i).equals("now"))
callableStatement.setTimestamp(i+1, Timestamp.valueOf(getCurrentDate()));
else
{
callableStatement.setString(i+1, parameters.get(i));
}
}
}
// execute insertDBUSER store procedure
Boolean results = callableStatement.execute();
int rowsAffected=0;
while(results||rowsAffected!=-1)
{
if(results)
{
rs=callableStatement.getResultSet();
break;
}
else
{
rowsAffected=callableStatement.getUpdateCount();
}
results= callableStatement.getMoreResults();
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return rs;
}