Enes Çalışkan
Enes Çalışkan

Reputation: 103

How can i get specific column from mySQL with JDBC?

    PreparedStatement ps2;
    ResultSet rs2;

    String username = jTextField_username.getText();
    String userpassword = jTextField_userpassword.getText();

    String sql = "SELECT distinct kalanizin from users where username =?";
    String[] resultsetarray = null;

    try {

       ps2 = MyConnection.getConnection().prepareStatement(sql);
       ps2.setString(1, username);

       rs2 = ps2.executeQuery();
       String result = rs2.getString(0); // i want this String to hold the first result

Hello everyone. I have one problem that keeps punching me in the head. As it says in the title , with using JDBC , i want to get the one column from my database. For example

Select * from users where username = "blabla"

I want Java to execute this query and get the first row it finds . Then puts into String or Integer. As you see my awful code on top, it fails to do that. I tried to accomplish this by using statement but it still didn't work. Can anyone help me? That would be much appreciated. Ps. sorry for my awful variable names and using my local language as variables

Upvotes: 2

Views: 5699

Answers (4)

sebastian
sebastian

Reputation: 2507

You are missing the ResultSet#next call.

According to your example code you can do this:

PreparedStatement ps2;
ResultSet rs2;

String username = jTextField_username.getText();
String userpassword = jTextField_userpassword.getText();

String sql = "SELECT distinct kalanizin from users where username =?";
String[] resultsetarray = null;

try {
   ps2 = MyConnection.getConnection().prepareStatement(sql);
   ps2.setString(1, username);

   rs2 = ps2.executeQuery();
   if(rs2.next()) { // moving to the first row
       String result = rs2.getString(1); // i want this String to hold the first result
   } else {
       // throw Exception ?
   }
} catch (SQLException e) {
    e.printStackTrace();
}

Upvotes: 2

user9869310
user9869310

Reputation:

My approach will be:

1)To limit the query. You can add limit as 1 to the query. I will encourage you to read about it rather than providing samples.

2 )Only fetch the columns you require, for example.

select u.age from users u where u.username=? 

And then depending upon the data type you can fetch it from the result set.

Upvotes: 2

Chira Cristian
Chira Cristian

Reputation: 31

Your are asking for the column with the columnIndex=0 but the column indexes start with 1

You could use this code maybe I used some time ago, to query your entire table

public List<Article> findAll() {
    Connection connection = jdbConnectionWrapper.getConnection();
    List<Article> articles = new ArrayList<>();
    try {
        PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM article");
        ResultSet resultSet = preparedStatement.executeQuery();

        while (resultSet.next()) {
            Article article = new Article();

            article.setId(resultSet.getLong(1));
            article.setName(resultSet.getString(2));
            article.setPrice(resultSet.getInt(3));

            articles.add(article);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return articles;
}

Upvotes: 3

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

If you know the column name You can use methods like getString(columnLabel) instead of getString(columnIndex):

Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.

columnLabel - the label for the column specified with the SQL AS clause. If the SQL AS clause was not specified, then the label is the name of the column

Upvotes: 3

Related Questions