在Java/MariaDb中使用preparedStatement,如下面的函数所示
public ArrayList<String> findPath(String roomName) throws SQLException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException
{
ArrayList<String> path = new ArrayList<String>();
connection = getConnection();
String queryPattern = "SELECT `Livello_1`, `Livello_2`, `Livello_3`, `Livello_4` FROM Camera WHERE Camera.Nome = '?'";
PreparedStatement queryStatement = connection.prepareStatement(queryPattern);
queryStatement.setString(1, roomName);
ResultSet rs = queryStatement.executeQuery();
if(rs.next())
{
for(int i = 0; i < 3; i++)
{
path.add(rs.getString(i));
}
}
return path;
}
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:
java.sql.SQLException:参数索引超出范围(1>参数个数,为0).
并且错误行号指向行
queryStatement.setString(1, roomName);
Run Code Online (Sandbox Code Playgroud)
正如评论中提到的,您应该删除 ? 中的引号。
另外, 中rs.getString(i)
,i
应为正值。循环从1开始计数。
总结一下:
public ArrayList<String> findPath(String roomName) throws SQLException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException
{
ArrayList<String> path = new ArrayList<String>();
connection = getConnection();
String queryPattern = "SELECT Livello_1, Livello_2, Livello_3, Livello_4 FROM Camera WHERE Camera.Nome = ?";
PreparedStatement queryStatement = connection.prepareStatement(queryPattern);
queryStatement.setString(1, roomName);
ResultSet rs = queryStatement.executeQuery();
if(rs.next())
{
for(int i = 1; i < 4; i++)
{
path.add(rs.getString(i));
}
}
return path;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
35769 次 |
最近记录: |