Results 1 to 3 of 3
  1. #1
    WishBone101 is offline Member
    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Default Multiple SELECT Query issue

    Hey everyone, I'm new to the forum and pretty new in java so forgive me and correct me on any of my mistakes.
    Anyway, I need to use multiple SELECT statements to retrieve data from a MySQL database but I'm getting issues. I can run a query using one SELECT statement and it works. Also, I added "?allowMultiQueries=true" in my connection statement which fixed some issues. Here is some of my code under the class Database.java

    public ResultSet getData() throws SQLException{
    String query = "USE snort_warehouse;" + "SELECT target_stats.server_id, target_stats.target, attack_stats.attacker, attack_stats.sig_name FROM target_stats JOIN attack_stats ON target_stats.target=attack_stats.target AND target_stats.interval_id=attack_stats.interval_id Where target_stats.interval_id>'2'";
    Statement stmt = con.createStatement();
    ResultSet rslt = stmt.executeQuery(query);

    and in my main I have:

    Database Warehouse=new Database();
    Warehouse.createConnection();
    ResultSet rslt=Warehouse.getData();
    while (rslt.next()){
    String server_id = rslt.getString("server_id");
    String target = rslt.getString("target");
    //I would continue this using all pieces of data grabbed from SQL statement
    System.out.print(server_id+"\t"+target +"\n");
    }

    My query using the join works in the MySQL browser but i get the following errors:
    Exception in thread "main" java.sql.SQLException: ResultSet is from UPDATE. No Data.
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:956)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:926)
    at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.ja va:7021)
    at SnortMonitor.main(SnortMonitor.java:15)

    any help would be great. again sorry for any rookie mistakes

  2. #2
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: Multiple SELECT Query issue

    Why don't you use multiple objects of PreparedStatement class for multiple queries.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Multiple SELECT Query issue

    Because one query is better than multiple ones.
    Do on the database what can be done on the database. Don't duplicate stuff the database is good at in Java.

    Anyway, for the OP, first off can you please use [code] tags [/code] when posting code.
    Second, that USE clause has turned your SQL into a CallableStatement. It is, in effect, a pseudo-procedure.
    And since your proceudre doesn't return a result set, you get that error.
    I would recommend removing the USE clause.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Replies: 1
    Last Post: 06-21-2011, 04:55 PM
  2. Problem executing SELECT query
    By mike_ledis in forum JDBC
    Replies: 2
    Last Post: 04-26-2011, 05:30 AM
  3. SELECT FROM WHERE query
    By herfnai in forum JDBC
    Replies: 3
    Last Post: 11-24-2008, 11:04 AM
  4. problem in select query
    By herfnai in forum JDBC
    Replies: 0
    Last Post: 08-17-2008, 11:47 AM
  5. Using a variable in a SELECT FROM WHERE query
    By cplmckenzie in forum JDBC
    Replies: 12
    Last Post: 04-23-2008, 03:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •