4 Replies - 1325 Views - Last Post: 11 April 2009 - 08:36 AM Rate Topic: -----

#1 Q8y   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 06-April 09

How can I retrieve SQL statement from the user ?

Posted 11 April 2009 - 02:22 AM

Hi

I want to know, how can I make the user can enter the SQL statement from himself ?? in this code he can't enter it. Only he can display the SQL that i wrote it...

this is my Code:
import java.sql.*;

public class db_testing {

	static final String DRIVER = "com.mysql.jdbc.Driver";			 
	static final String DATABASE_URL = "jdbc:mysql://localhost/S204111933";

	public static void main(String[] args) {

		Connection cn=null;
		Statement st= null;
		ResultSet rset=null;

		try{
			Class.forName(DRIVER);
			
			cn=DriverManager.getConnection(DATABASE_URL, "root", "admin");
			st=cn.createStatement();
			rset=st.executeQuery("select * from employee");
			
			ResultSetMetaData metadata=rset.getMetaData();
			System.out.println("The begining: ");
			
			for(int i=1;i<=metadata.getColumnCount();i++)
			System.out.print(metadata.getColumnName(i)+"\t");
			
			System.out.println();
			System.out.println();
			
			
			while(rset.next()){
			for(int i=1;i<=metadata.getColumnCount();i++)
			System.out.print(rset.getObject(i)+"\t\t");
			System.out.println();}

			
		}
		
		
		
		
		catch(Exception e){
			e.printStackTrace();
		}
		
		finally{
			
			try{
			cn.close();
			st.close();
			rset.close();
		}
			catch(Exception e1){
				e1.printStackTrace();
			}

		
	}

}
}



thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: How can I retrieve SQL statement from the user ?

#2 Hurshai   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 51
  • Joined: 14-September 08

Re: How can I retrieve SQL statement from the user ?

Posted 11 April 2009 - 02:58 AM

So you want the user to enter their own query? In that case you want to import the scanner and create a string that is passed to:

 rset = st.executeQuery("userQuery");


This post has been edited by Hurshai: 11 April 2009 - 03:05 AM

Was This Post Helpful? 1
  • +
  • -

#3 Q8y   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 06-April 09

Re: How can I retrieve SQL statement from the user ?

Posted 11 April 2009 - 03:28 AM

View PostHurshai, on 11 Apr, 2009 - 01:58 AM, said:

So you want the user to enter their own query? In that case you want to import the scanner and create a string that is passed to:

 rset = st.executeQuery("userQuery");




I did already that , and it's not working ... it says, error in your SQL syntax, and i did write the same sql statement that i wrote it before and it works(select * from employee)...

this is my new code after i did what u say...
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Scanner;


public class DB_1_GUI {
	
	static final String DRIVER = "com.mysql.jdbc.Driver";			 
	static final String DATABASE_URL = "jdbc:mysql://localhost/S204111933";

	public static void main(String[] args) {

		Connection cn=null;
		Statement st= null;
		ResultSet rset=null;

		try{
			Class.forName(DRIVER);
			
			cn=DriverManager.getConnection(DATABASE_URL, "root", "admin");
			st=cn.createStatement();
			
			Scanner input= new Scanner(System.in);
			System.out.println("Enter the SQL statement: ");
			String s1=input.next();
			
			rset=st.executeQuery(s1);
			
			ResultSetMetaData metadata=rset.getMetaData();
			System.out.println("The begining: ");
			
			for(int i=1;i<=metadata.getColumnCount();i++)
			System.out.print(metadata.getColumnName(i)+"\t");
			
			System.out.println();
			System.out.println();
			
			
			while(rset.next()){
			for(int i=1;i<=metadata.getColumnCount();i++)
			System.out.print(rset.getObject(i)+"\t\t");
			System.out.println();}

			
		}
		
		
		
		
		catch(Exception e){
			e.printStackTrace();
		}
		
		finally{
			
			try{
			cn.close();
			st.close();
			rset.close();
		}
			catch(Exception e1){
				e1.printStackTrace();
			}

		
	}

}
}


Was This Post Helpful? 0
  • +
  • -

#4 Q8y   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 06-April 09

Re: How can I retrieve SQL statement from the user ?

Posted 11 April 2009 - 04:10 AM

Thanks Hurshai...

i got the problem and i fixed it, i was getting only one string, i changed it to take line and it's fixed... i didn't realise that it was getting only one word ;p


Thanks again ;)
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: How can I retrieve SQL statement from the user ?

Posted 11 April 2009 - 08:36 AM

View PostQ8y, on 11 Apr, 2009 - 03:10 AM, said:

Thanks Hurshai...

i got the problem and i fixed it, i was getting only one string, i changed it to take line and it's fixed... i didn't realise that it was getting only one word ;p


Thanks again ;)

One note here:
better to use PreparedStatement
I hope you realize that the use can enter: delete * from tableName;

Better to have PreparedStatement like: "Select * from tableName where Name='?';"
and just prompt for Name
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1