简体   繁体   中英

Java MySQL How to select rows within a ResultSet?

I have a very basic MySQL Database. This is the query I used to create it:

CREATE TABLE SC_Users(
ID int(11) NOT NULL AUTO_INCREMENT, 
PRIMARY KEY (ID), 
Username varchar(255) NOT NULL, 
Class varchar(255) NOT NULL
)

I get a ResultSet by querying my database with this:

SELECT * FROM SC_Users

I want to be able to search for a username and then find the corresponding class of that username. How would I do this without using a different query?

I know its possible to find it easily by using:

SELECT * FROM SC_Users WHERE `Username` = Username

But I would prefer to use only 1 query if possible to find any value of a user I want. Thanks in advance for the help!

You can simply use PreparedStatement for your purpose.

    PreparedStatement pst=null;
    ResultSet rs=null;
    String userName = "someUser";
    String userClass;
    pst=con.prepareStatement("select * from SC_users where Username=? ");
    pst.setString(1,userName);
    rs=pst.executeQuery();
        if(rs.next())
        {
            userClass=rs.getString("Class");
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM