I have below code working for me.
import java.sql.*;
class MyClass {
public static void main (String[] Owner ) throws Exception
{
Class.forName ("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@//host:port/SID", "username", "password");
try {
Statement stmt = conn.createStatement();
try {
for (int i=0; i < owner.length; i++){
String consumerName = "TestConsumer";
ResultSet msgs = Stmt.executeQuery("select msg_id from Table where owner = '" + owner[i] + "' and consumer_name = '" + consumerName + "' and msg_state = 'READY'" );
while (msgs.next())
System.out.println (msgs.getString(1));
try { msgs.close(); } catch (Exception closeMsgsExcp) {}
}
}
finally {
try { stmt.close(); } catch (Exception closeStmtExcp) {}
}
}
finally {
try { conn.close(); } catch (Exception closeConnExcp) {}
}
}
}
But when i try to change this code to the following i get an error - Error: Main method not found in class MyClass, please define the main method as: public static void main(String[] args). I need to receive owner and consumerName as arguments/input for my program.
import java.sql.*;
class MyClass {
public static void main (String[] Owner, String[] consumerName ) throws Exception
{
Class.forName ("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@//host:port/SID", "username", "password");
try {
Statement stmt = conn.createStatement();
try {
for (int i=0; i < owner.length; i++){
//String consumerName = "TestConsumer";
ResultSet msgs = Stmt.executeQuery("select msg_id from Table where owner = '" + owner[i] + "' and consumer_name = '" + consumerName[i] + "' and msg_state = 'READY'" );
while (msgs.next())
System.out.println (msgs.getString(1));
try { msgs.close(); } catch (Exception closeMsgsExcp) {}
}
}
finally {
try { stmt.close(); } catch (Exception closeStmtExcp) {}
}
}
finally {
try { conn.close(); } catch (Exception closeConnExcp) {}
}
}
}
, how can this be done?
mainacceptsString[] args. You want to pass two strings: owner and consumer. They will beargs[0]andargs[1]respectively.mainmethod should always have just a single parameter, of typeString[]. Do you actually need multiple owners and multiple consumers? If not, just usepublic static void main(String[] args)and thenString owner = args[0]; String consumer = args[1];. Next, you should definitely read up on parameterized SQL rather than embedding the value directly into your SQL.