Results 1 to 3 of 3
Thread: Multiple SELECT Query issue
- 06-18-2012, 07:33 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 1
- Rep Power
- 0
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
- 06-19-2012, 10:12 AM #2
Re: Multiple SELECT Query issue
Why don't you use multiple objects of PreparedStatement class for multiple queries.
- 06-19-2012, 10:41 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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
-
How to create a header from an sql select query in java
By renu in forum New To JavaReplies: 1Last Post: 06-21-2011, 04:55 PM -
Problem executing SELECT query
By mike_ledis in forum JDBCReplies: 2Last Post: 04-26-2011, 05:30 AM -
SELECT FROM WHERE query
By herfnai in forum JDBCReplies: 3Last Post: 11-24-2008, 11:04 AM -
problem in select query
By herfnai in forum JDBCReplies: 0Last Post: 08-17-2008, 11:47 AM -
Using a variable in a SELECT FROM WHERE query
By cplmckenzie in forum JDBCReplies: 12Last Post: 04-23-2008, 03:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks