Please help getting error while running regarding result set

package databaseeditor;

import java.io.Serializable;

import java.sql.*;

import java.*;

publicclass DBEditorimplements Serializable{

String query;

StringBuffer results;

public DBEditor(){

}

public String getResults(){

return results.toString();

}

publicvoid doQuery(String query, String driver, String url){

try{

Class.forName(driver);

Connection connection = DriverManager.getConnection(url);

Statement statement = connection.createStatement();

this.query = query;

if (query.toLowerCase().startsWith("select")){

ResultSet resultSet = statement.executeQuery(query);

results =new StringBuffer();

ResultSetMetaData metaData = resultSet.getMetaData();

int numberOfColumns = metaData.getColumnCount();

for (int i = 1; i <= numberOfColumns; i++)

results.append(metaData.getColumnName(i) +"\t");

results.append("\n");

while (resultSet.next()){

for (int i = 1; i <= numberOfColumns; i++)

results.append(resultSet.getObject(i) +"\t");

results.append("\n");

}

}

else{

statement.executeUpdate(query);

this.query = query;

ResultSet resultSet = statement.executeQuery(query);

}

statement.close();

connection.close();

}// end try

catch(SQLException excep){

excep.printStackTrace();

}

catch(ClassNotFoundException classexcep){

classexcep.printStackTrace();

}// end catch

}

}

Main class

package databaseeditor;

/**

*

* */

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.*;

import javax.swing.*;

// import dbeditor.*;

publicclass Mainextends JFrameimplements ActionListener{

JTextField driverField, urlField, useridField, passwordField;

JTextArea queryArea, display;

JPanel controls, combinedPanel, queryPanel, miscInfoPanel;

JButton execute, exit;

DBEditor runQuery;

/**

* Creates a new instance of Main

*/

public Main(){

super("Database Info");

setupGUI();

runQuery =new DBEditor();

}

publicvoid setupGUI(){

miscInfoPanel =new JPanel();

miscInfoPanel.setLayout(new GridLayout(2, 2));

driverField =new JTextField("sun.jdbc.odbc.JdbcOdbcDriver");

driverField.setBorder(BorderFactory.createTitledBorder("Driver"));

miscInfoPanel.add(driverField);

System.out.println("driver is" + driverField);

urlField =new JTextField("jdbc:odbc:Quals");

urlField.setBorder(BorderFactory.createTitledBorder("Database URL"));

miscInfoPanel.add(urlField);

useridField =new JTextField();

useridField.setBorder(BorderFactory.createTitledBorder("User ID"));

miscInfoPanel.add(useridField);

passwordField =new JTextField();

passwordField.setBorder(BorderFactory.createTitledBorder("Password"));

miscInfoPanel.add(passwordField);

queryArea =new JTextArea();

queryArea.setBorder(BorderFactory.createTitledBorder("DatabaseQuery"));

combinedPanel =new JPanel(new GridLayout(1, 2));

combinedPanel.add(miscInfoPanel);

combinedPanel.add(queryArea);

combinedPanel.setBorder(BorderFactory

.createTitledBorder("Database Info"));

add(combinedPanel, BorderLayout.NORTH);

display =new JTextArea();

display.setBorder(BorderFactory.createTitledBorder("DataDisplay"));

add(new JScrollPane(display), BorderLayout.CENTER);

execute =new JButton("Run Query");

execute.addActionListener(this);

exit =new JButton("Exit");

exit.addActionListener(this);

controls =new JPanel();

controls.add(execute);

controls.add(exit);

controls.setBorder(BorderFactory.createTitledBorder("CONTROLS"));

add(controls, BorderLayout.SOUTH);

setSize(600, 600);

setLocation(50, 20);

setVisible(true);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

publicvoid actionPerformed(ActionEvent e){

if (e.getSource() == execute){

if (queryArea.getText().trim().equals("")){

JOptionPane.showMessageDialog(this,"Enter the Database query");

}else{

runQuery.doQuery(queryArea.getText(), driverField.getText(),

urlField.getText());

if (queryArea.getText().toLowerCase().startsWith("select")){

display.setText(runQuery.getResults());

}elseif (queryArea.getText().toLowerCase().startsWith(

"insert")){

JOptionPane.showMessageDialog(this,"insertion completed");

String queryString = getSelectQuery(queryArea.getText());

runQuery.doQuery(queryString, driverField.getText(), urlField.getText());

display.setText(runQuery.getResults());

}elseif (queryArea.getText().toLowerCase().startsWith(

"update")){

String queryString = getSelectQuery(queryArea.getText());

runQuery.doQuery(queryString, driverField.getText(), urlField.getText());

display.setText(runQuery.getResults());

}elseif (queryArea.getText().toLowerCase().startsWith(

"delete")){

String queryString = getSelectQuery(queryArea.getText());

runQuery.doQuery(queryString, driverField.getText(), urlField.getText());

display.setText(runQuery.getResults());

}

}

}else

System.exit(0);

}

publicstaticvoid main(String[] args){

new Main();

}

private String getSelectQuery(String queryString){

String tableName =new String();

CharSequence cust1 =new String("customers");

CharSequence cust2 =new String("Customers");

CharSequence cust3 =new String("CUSTOMERS");

CharSequence rate1 =new String("rates");

CharSequence rate2 =new String("Rates");

CharSequence rate3 =new String("RATES");

CharSequence project1 =new String("projects");

CharSequence project2 =new String("Projects");

CharSequence project3 =new String("PROJECTS");

if(queryString.contains(cust1)||queryString.contains(cust2)||queryString.contains(cust3)){

tableName ="customers";

}

if(queryString.contains(rate1)||queryString.contains(rate2)||queryString.contains(rate3)){

tableName ="rates";

}

if(queryString.contains(project1)||queryString.contains(project2)||queryString.contains(project3)){

tableName ="projects";

}

System.out.println("select * from "+tableName);

return"select * from "+tableName;

}

}

It displays a window in which Sql commands can be entered .

WHen I run select * from customers ;

It runs fine.

But when I run update it gives me an error that no resultset was produced.

Please help

[12266 byte] By [maggiemaggiea] at [2007-11-26 21:01:08]
# 1

Because an update statement doesn't return a result set.

If you really want to do something like this, then you are going to need to check whether the first word in the query is "select" or not. If it is select, then use executeQuery and get a ResultSet. If it is not, use executeUpdate and retrieve an int that indicates the number of affected rows.

This, of course, disregards Stored Procedures and the like. Also, note, that I hope this is only a little test thing to play around with JDBC and that the code (and the principle) never finds any active use anywhere. This is just the sort of thing to really screw up your Database (and cause many other problems as well).

Edit: Forget the second paragraph. Didn't really look at the code. The first and third paragraphs are still valid, however.

masijade.a at 2007-7-10 2:32:26 > top of Java-index,Java Essentials,New To Java...
# 2

When inserting/updating/deleting you should call executeUpdate, not executeQuery

http://java.sun.com/j2se/1.4.2/docs/api/java/sql/PreparedStatement.html#executeUpdate()

Just checked you already call executeUpdate but afterwards you also call executeQuery. Remove the second call and it should be ok ;-)

Just for curiosity: why do you keep the sent query to runQuery as a member? :-p

Message was edited by:

benubach

Message was edited by:

benubach

benubacha at 2007-7-10 2:32:26 > top of Java-index,Java Essentials,New To Java...
# 3

what exact error you are getting.

Seeing your post it seems you are trying to obtain a resultset from an update query.

Try to use the executeUpdate without trying to get a result set.

You have done so in line number 46.. as below

ResultSet resultSet = statement.executeQuery(query);

Change it to :

statement.executeUpdate(query);

Hope this helps

Rgds

ayusman_dikshita at 2007-7-10 2:32:26 > top of Java-index,Java Essentials,New To Java...
# 4
That doesnt work as well.
maggiemaggiea at 2007-7-10 2:32:26 > top of Java-index,Java Essentials,New To Java...
# 5
Error is when I execute Select command it displays the data.But when I execute update command it says that result set has not been produced.I am not able to figure the error.
maggiemaggiea at 2007-7-10 2:32:26 > top of Java-index,Java Essentials,New To Java...
# 6

> Error is when I execute Select command it displays

> the data.

>

> But when I execute update command it says that result

> set has not been produced.

>

> I am not able to figure the error.

I'm sorry, but, what is so hard to understand. An update does not produce a ResultSet. It updates rows in the DB and simply returns an integer indicating the number of affected rows. So, of course, a ResultSet will not be produced.

masijade.a at 2007-7-10 2:32:26 > top of Java-index,Java Essentials,New To Java...
# 7
In your else statement you have:statement.executeUpdate(query);this.query = query; ResultSet resultSet = statement.executeQuery(query);Take a close look at the third line. Do you now see why this error might be occurring?
masijade.a at 2007-7-10 2:32:26 > top of Java-index,Java Essentials,New To Java...
# 8
I tried this.query = query; ResultSet resultSet = statement.executeUpdate(query);statement.executeUpdate(query); doesnt work gives an error.
maggiemaggiea at 2007-7-10 2:32:26 > top of Java-index,Java Essentials,New To Java...
# 9

Because executeUpdate (just like an update/delete sql statement itself) returns an integer (a number) indicating the number of rows affected. It does not return a ResultSet. Simply change the entire else to the following:

int result = statement.executeUpdate(query);

this.query = query; // I really don't know why you're doing this, but okay.

results = new StringBuffer();

results.append(String.valueOf(result)).append(" affected rows.\n");

masijade.a at 2007-7-10 2:32:26 > top of Java-index,Java Essentials,New To Java...
# 10
THANKYOU SO MUCHIt worked.Thanks alott.
maggiemaggiea at 2007-7-10 2:32:26 > top of Java-index,Java Essentials,New To Java...
# 11
Errrmmmm... man, dukes please? There were at least 5 correct and helpful answers in the first 5 replies :-/
benubacha at 2007-7-10 2:32:26 > top of Java-index,Java Essentials,New To Java...