-2

我的方法应该从我username的:passwordadmintable

MySql 控制台中的我的表:

在此处输入图像描述

我的方法:

    public void adminEnterCondition(){
    Connection con;
    PreparedStatement preparedStatement;
    ResultSet resultSet;
    String query = "select * from admintable";
    String username,password;
    try {
        con= DriverManager.getConnection(...);
        preparedStatement = con.prepareStatement(query);
        resultSet = preparedStatement.executeQuery();
        username = ... 
        password = ...

    }
    catch (SQLException sqle){
        sqle.printStackTrace();
    }
}

更新:

我试试这个:

    public void adminEnterCondition() {
    Connection con;
    PreparedStatement preparedStatement;
    ResultSet resultSet;
    String query = "select * from admintable";
    String username = null, password = null;
    try {
        con = DriverManager.getConnection(...);
        System.out.println("Connected");
        preparedStatement = con.prepareStatement(query);
        resultSet = preparedStatement.executeQuery();

        while (resultSet.next()) {
            username = resultSet.getString(username);
            password = resultSet.getString(password);
        }

        System.out.println(username);
        System.out.println(password);

    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
}

但是会发生这种异常:

Connected
Exception in thread "main" java.lang.NullPointerException
at java.lang.String$CaseInsensitiveComparator.compare(String.java:1176)
at java.lang.String$CaseInsensitiveComparator.compare(String.java:1170)
at java.util.TreeMap.getEntryUsingComparator(TreeMap.java:369)
at java.util.TreeMap.getEntry(TreeMap.java:340)
at java.util.TreeMap.get(TreeMap.java:273)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1072)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5488)
at test.GetCellvalue.adminEnterCondition(GetCellvalue.java:26)
at test.GetCellvalue.main(GetCellvalue.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
4

2 回答 2

2

使用这种方式检索

while(resultSet.next())
{
username=resultSet .getString("username");
password=resultSet .getString("password");
}

OtherWay 除了写列名,你也可以像这样写列号

while(resultSet.next())
    {
    username=resultSet .getString(1);
    password=resultSet .getString(2);
    }
于 2013-09-15T16:39:09.310 回答
1

您可以通过列名从 ResultSet 中检索这些字符串:

    String username = resultSet.getString("username");
    String password = resultSet.getString("password");
于 2013-09-15T16:36:45.610 回答