猿问

从数据库中获取信息以显示在项目上

我试图从数据库中获取 sql 中的信息,每列中有两个条目,但是使用以下代码,我可以获得第一个条目信息,如果我想获取第二个条目的记录并在项目中显示它如何我会得到吗?


    String username1= rs.getString ("USERNAME");

    String password1= rs.getString ("PASSWORD");

    String aname1= rs.getString ("a_name");

    String aname2= rs.getString ("a_name");


    System.out.print("Enter Your Username: ");

    String usernamea=(br.readLine());

    System.out.print("Enter Your Password: ");

    String passworda=(br.readLine());

    if ((usernamea.equals(username1) && (passworda.equals(password1))))

    {

        System.out.println("Login Success! Welcome "+aname1+"!");

        System.out.println("You are granted with Admin Acess!");

        rs.close();

    }

    else if ((usernamea.equals(username2) && (passworda.equals(password2))))

    {

        System.out.println("Login Success! Welcome Guest User!");

        rs.close();

    }


偶然的你
浏览 142回答 3
3回答

侃侃无极

我不太确定您在问什么,也不确定您发布的代码实际上与您要问的内容有何关系。当您说“您想要第二个条目”时,我假设您指的是 SQL 表中的第二个记录。try(Connection conn = DriverManager.getConnection(url, username, password)){Statement stat = conn.createStatement();ResultSet rs = stat.executeQuery("SELECT * FROM [TABLENAME]");rs.next();//All data stored in the ResultSet obj, to which you may loop through & select rows of interest. Particularly useful methods are .getMetaData(), .getRow(), etc.}希望这就是你所要求的。

犯罪嫌疑人X

考虑你的代码:&nbsp; &nbsp; ResultSet rs = stat.executeQuery("SELECT * FROM [TABLENAME]");&nbsp; &nbsp; String username1 = rs.getString ("USERNAME");&nbsp; &nbsp; String password1 = rs.getString ("PASSWORD");&nbsp; &nbsp; String aname1 = rs.getString ("a_name");&nbsp; &nbsp; rs.next();&nbsp; &nbsp; String username2 = rs.getString ("USERNAME");&nbsp; &nbsp; String password2 = rs.getString ("PASSWORD");&nbsp; &nbsp; String aname2 = rs.getString ("a_name");现在您可以检查第一个条目和第二个条目。然而,更好的做法是创建一个用户对象, save username,password并aname在其中。然后,使用一些循环,您可以遍历整个结果集并从中获取所有信息。&nbsp; &nbsp; ResultSet rs = stat.executeQuery("SELECT * FROM [TABLENAME]");&nbsp; &nbsp; List<User> userList = new ArrayList<User>();&nbsp; &nbsp; while(rs.next()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; User user = new User();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user.setUsername(rs.getString ("USERNAME"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user.setPassword(rs.getString ("PASSWORD"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user.setAname(rs.getString ("a_name"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userList.add(user);&nbsp; &nbsp; }

狐的传说

如果您已经有一个 ResultSet rs,则可以调用 rs.next()。例如一些代码:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (rs.next())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.name = rs.getString(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.creator = rs.getString(2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.dbname = rs.getString(3);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.tsname = rs.getString(4);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.cardf = rs.getFloat(5);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.npages = rs.getInt(6);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.statstime = rs.getString(7);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.avgrowlen = rs.getInt(8);&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答