Handling dynamic SQL

import Java.sql.*;

public class UnknownQuery

{

public static void main(String args[]) throws Exception

{

Class. forName("oracle.jdbc. driver. OracleDriver");

System.out.println("Driver loaded…");

String url = "jdbc:oracle;thin:@localhosUJ521:orcl";

Connection con = DriverManager.getConnection(url,"scott","tiger");

System.out.println("Connection is established…..");

Statement st=con.createStatement();

String sql=args[0];

if(st.execute(sql))

{

ResultSet rs = st.getResultSet(); while(rs.next())

System.out.println(rs.getString(l)+" "+rs.getString(2)+" "+rs.getString(3));

rs.close();

}

else

System.out.println("Number of rows updated:"+st.getUpdateCount());

st.close();

con.close();

System.out.println("Connection closed….");

}//main()

}//class

The above application takes the SQL statement from the command prompt. The implementation can handle any kind of SQL statement dynamically. execute() of Statement object can submit any kind of SQL statement to the database. If the submitted statement produces ResultSet, that method returns true. This feature is used to handle the SQL statement dynamically.