Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: SQL in Java

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    19
    Thanks
    2
    Thanked 1 Time in 1 Post

    Question SQL in Java

    java.sql.SQLException: Invalid column index

    Hi i am trying to retrieve some data from the database using the following code

    public class Database {
     
     
    	public static void main(String arg[]) throws Throwable
     
    	{
    		Database db=new Database();
    		db.execute();
     
    		}
    	public  static Connection getOracleConnection() throws Exception {
    		String driver = "oracle.jdbc.driver.OracleDriver";
    		String url = "dbc:oracle:thin:@10.118.0.22:1521:DEVCENO3";
    		String username = "cenevo";
    		String password = "cenevo";
            System.out.println("Connected");
    		Class.forName(driver); // load Oracle driver
    		Connection conn = DriverManager.getConnection(url, username, password);
    		System.out.println("Connected");
    		return conn;
    	}
     
    	public void execute() throws Exception{
    		Connection conn = null;
    		Statement stmt = null;
    		ResultSet rs = null;
    		int output=0;
    		conn = getOracleConnection();
    		stmt = conn.createStatement();
    		rs = stmt.executeQuery("SELECT DISTINCT BUSID FROM LNKBUSEXEC WHERE EXECID =8");
     
    		System.out.println(rs.getInt(0));
     
     
    		while (rs.next()) {
    			ArrayList ret=new ArrayList();
    			Connection conn1 = null;
    			Statement stmt1 = null;
    			ResultSet rs1 = null;
    			int output1=0;
    			conn1 = getOracleConnection();
    			stmt1 = conn.createStatement();
    			rs1 = stmt.executeQuery("SELECT AWSWSDL FROM ASPWEBSERVICE WHERE AWSIDENT = (SELECT ATSWISDENT FROM ASPTRANSACTION WHERE ATSCINT = (SELECT APACINT FROM ASPLNKPANELACTION WHERE (APACINA = (SELECT APACINA FROM LNKBUSTRAPAACT WHERE BUSID = (SELECT BUSID FROM BUSINESSSCENARIO WHERE BUSID = '"+rs.getInt(1)+"' ))) AND (APACINT = (SELECT APACINT FROM LNKBUSTRAPAACT WHERE BUSID = (SELECT BUSID FROM BUSINESSSCENARIO WHERE BUSID = '"+rs.getInt(1)+"' ))) AND (APACINP = (SELECT APACINP FROM LNKBUSTRAPAACT WHERE BUSID = (SELECT BUSID FROM BUSINESSSCENARIO WHERE BUSID = '"+rs.getInt(1)+"' )))))");
     
    			System.out.println(rs1.getString(1));
    		}
     
     
    	}
     
     
    }
    i am encountering the exception as follows,

    Exception in thread "main" java.sql.SQLException: Invalid column index
    at oracle.jdbc.driver.DatabaseError.throwSqlException (DatabaseError.java:112)
    at oracle.jdbc.driver.DatabaseError.throwSqlException (DatabaseError.java:146)
    at oracle.jdbc.driver.DatabaseError.throwSqlException (DatabaseError.java:208)
    at oracle.jdbc.driver.OracleResultSetImpl.getInt(Orac leResultSetImpl.java:503)
    at com.gold_solutions.http://www.webservices.Database.exec...tabase.java:48)
    at com.gold_solutions.www.webservices.Database.main(Database.java:23)


    Please help me...
    Last edited by Sai; March 31st, 2010 at 01:39 AM.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: SQL in Java

    Hey,
    Check if this "select" is returning something.

    If you don't solve your problem.. post your table.

    =P

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,608
    Thanks
    24
    Thanked 316 Times in 295 Posts

    Default Re: SQL in Java

    if your problem is
    rs = stmt.executeQuery("SELECT DISTINCT BUSID FROM LNKBUSEXEC WHERE EXECID =8");

    Keep in mind that tables/columns/ect. with multi-word names need to be treated differently than tables/columns/ect. with just one-word names. Specifically, you need to add quotes before and after the names. In JAVA, you add a quote to a String with the symbol: \". So, try your code with this line instead:
    rs = stmt.executeQuery("SELECT \"DISTINCT BUSID\" FROM LNKBUSEXEC WHERE EXECID =8");
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/