Skip to content
Advertisement

How do i get a result from a query? (Java, SQL) [closed]

package sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {

    public static void main(String[] args) {
        String query = "show tables;";
        
        try {
            Connection con = DriverManager.getConnection("jdbc:mysql://IP:PORT/DATABASE", "USER", "PASSWORD");
            
            Statement stmt = con.createStatement();
            
            ResultSet rs = stmt.executeQuery(query);
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

How can i get a result of my executed query? I filled the IP, PORT, DATABASE, USER and PASSWORD. I just get no output. Thanks for every help!

Advertisement

Answer

You need to actually read the ResultSet. For example:

ResultSet rs = stmt.executeQuery(query);

// Add the section below

while (rs.next()) {
  String x = rs.getString(1);
  System.out.println(x);
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement