Sqite选择查询行不起作用

时间:2015-12-24 11:18:55

标签: java

我使用JDBC从sqlite数据库中获取数据。在DB我有3列 - 登录,密码和角色。我尝试通过登录查找行,但它不起作用,当我尝试getString("password")"role"时,我有一个例外,错误在哪里?感谢

resSet = statmt.executeQuery("SELECT * FROM users WHERE login='"+login+"';");
if( hasUser( login)){
    System.out.println("User finded:");
    while(resSet.next()) {
        System.out.println("login = " + resSet.getString("login"));
        // !exeption   
        System.out.println("password = " + resSet.getString("password"));
        // !exeption   
        System.out.println("role = " + resSet.getString("role"));
        System.out.println();
    }
}else{
    System.out.println( "User not found");
}

2 个答案:

答案 0 :(得分:0)

ResultSetMetaData metaData = resultSet.getMetaData();
int count = metaData.getColumnCount(); //number of column
String columnName[] = new String[count];

for (int i = 1; i <= count; i++)
{
  metaData.getColumnLabel(i)); 
}

请参阅此ans..ans,查看结果集中列的名称 http://stackoverflow.com/questions/19094999/java-how-to-get-column-name-on-result-set

答案 1 :(得分:0)

您可以检查以查看返回的列名。

ResultSetMetaData rsmd = resSet.getMetaData();
int colCount = rsmd.getColumnCount();

String rValue = "";
for (int i = 1; i <= colCount ; i++){

    String name = rsmd.getColumnName(i);
    rValue += name + " ";
}

System.out.println(rValue);