Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. GETTING EXCEPTION ON JDBC TRANSACTION MGMT --->java.sql.SQLException: Invalid column index:at LINE 8 & 12

GETTING EXCEPTION ON JDBC TRANSACTION MGMT --->java.sql.SQLException: Invalid column index:at LINE 8 & 12

Scheduled Pinned Locked Moved Database
databasejavacomquestionannouncement
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LOKENDRA YADAV
    wrote on last edited by
    #1

    -----------------------------------DATABASE------------------------------------------------------------- CREATE TABLE ACCOUNTS(ACCNO INT PRIMARY KEY,BAL PRIMARY KEY); insert into accounts values(77,10000); insert into accounts values(88,10000); ---------------------------------HERE IS JDBC CODE---------------------------------------------------- import A.JDBCUtil; // ****JDBCUtil is the class having connection code import java.sql.*; public class Test12 { public static void main(String[] args) { Account acc=new Account(); acc.transfer(77,88,5000); //******LINE 8****** } } class InvalidAccountNumberException extends Exception{ int accno; public InvalidAccountNumberException() { } public InvalidAccountNumberException(int accno){ this.accno=accno; } @Override public String toString() { return "Accno"+accno+"not found"; } } class InsufficientFundsException extends Exception{ public InsufficientFundsException() { } @Override public String toString() { return "sufficent funds are not available"; } } class Account{ int bal; int sab,dab,danb,sanb; public void transfer(int sa,int da,int amt){ Connection con=null; PreparedStatement ps1=null; PreparedStatement ps2=null; try{ con=JDBCUtil.getOracleConnection(); con.setAutoCommit(false); ps1=con.prepareStatement("select bal from accounts where accno='?'"); ps1.setInt(1,da); ******//LINE 12****** ResultSet rs1=ps1.executeQuery(); if(rs1.next()) dab=rs1.getInt(1); else throw new InvalidAccountNumberException(da); danb=dab+amt; //updating destination account balance ps2=con.prepareStatement("update accounts set bal='?' where accno='?' "); ps2.setInt(1,danb); ps2.setInt(2,da); ps2.executeUpdate(); System.out.println("**"+da+"*updated*"); //checking source account ps1.setInt(1,sa); rs1=ps1.executeQuery(); if(rs1.next()){ sanb=rs1.getInt(1); }else{ throw new InvalidAccountNumberException(); } if(sab>=amt){ sanb=sab-amt; }else{ throw new InsufficientFundsException(); } //updating Source account ps2.setInt(1,sanb); ps2.setInt(2,sa); ps2.executeUpdate(); con.commit(); System.out.println("**"+sa+"updated"); System.out.println("funds transferred"); }catch(Exception e){ e.printS

    L 1 Reply Last reply
    0
    • L LOKENDRA YADAV

      -----------------------------------DATABASE------------------------------------------------------------- CREATE TABLE ACCOUNTS(ACCNO INT PRIMARY KEY,BAL PRIMARY KEY); insert into accounts values(77,10000); insert into accounts values(88,10000); ---------------------------------HERE IS JDBC CODE---------------------------------------------------- import A.JDBCUtil; // ****JDBCUtil is the class having connection code import java.sql.*; public class Test12 { public static void main(String[] args) { Account acc=new Account(); acc.transfer(77,88,5000); //******LINE 8****** } } class InvalidAccountNumberException extends Exception{ int accno; public InvalidAccountNumberException() { } public InvalidAccountNumberException(int accno){ this.accno=accno; } @Override public String toString() { return "Accno"+accno+"not found"; } } class InsufficientFundsException extends Exception{ public InsufficientFundsException() { } @Override public String toString() { return "sufficent funds are not available"; } } class Account{ int bal; int sab,dab,danb,sanb; public void transfer(int sa,int da,int amt){ Connection con=null; PreparedStatement ps1=null; PreparedStatement ps2=null; try{ con=JDBCUtil.getOracleConnection(); con.setAutoCommit(false); ps1=con.prepareStatement("select bal from accounts where accno='?'"); ps1.setInt(1,da); ******//LINE 12****** ResultSet rs1=ps1.executeQuery(); if(rs1.next()) dab=rs1.getInt(1); else throw new InvalidAccountNumberException(da); danb=dab+amt; //updating destination account balance ps2=con.prepareStatement("update accounts set bal='?' where accno='?' "); ps2.setInt(1,danb); ps2.setInt(2,da); ps2.executeUpdate(); System.out.println("**"+da+"*updated*"); //checking source account ps1.setInt(1,sa); rs1=ps1.executeQuery(); if(rs1.next()){ sanb=rs1.getInt(1); }else{ throw new InvalidAccountNumberException(); } if(sab>=amt){ sanb=sab-amt; }else{ throw new InsufficientFundsException(); } //updating Source account ps2.setInt(1,sanb); ps2.setInt(2,sa); ps2.executeUpdate(); con.commit(); System.out.println("**"+sa+"updated"); System.out.println("funds transferred"); }catch(Exception e){ e.printS

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Where is line 8, where is line 12?

      L 2 Replies Last reply
      0
      • L Lost User

        Where is line 8, where is line 12?

        L Offline
        L Offline
        LOKENDRA YADAV
        wrote on last edited by
        #3

        CHECK THE CODE I HAVE WRITTEN THE LINE NO ON THE CODE

        1 Reply Last reply
        0
        • L Lost User

          Where is line 8, where is line 12?

          L Offline
          L Offline
          LOKENDRA YADAV
          wrote on last edited by
          #4

          -----------------------------------DATABASE------------------------------------------------------------- CREATE TABLE ACCOUNTS(ACCNO INT PRIMARY KEY,BAL PRIMARY KEY); insert into accounts values(77,10000); insert into accounts values(88,10000); ---------------------------------HERE IS JDBC CODE---------------------------------------------------- import A.JDBCUtil; // ****JDBCUtil is the class having connection code import java.sql.*; public class Test12 { public static void main(String[] args) { Account acc=new Account(); acc.transfer(77,88,5000); //******LINE 8****** } } class InvalidAccountNumberException extends Exception{ int accno; public InvalidAccountNumberException() { } public InvalidAccountNumberException(int accno){ this.accno=accno; } @Override public String toString() { return "Accno"+accno+"not found"; } } class InsufficientFundsException extends Exception{ public InsufficientFundsException() { } @Override public String toString() { return "sufficent funds are not available"; } } class Account{ int bal; int sab,dab,danb,sanb; public void transfer(int sa,int da,int amt){ Connection con=null; PreparedStatement ps1=null; PreparedStatement ps2=null; try{ con=JDBCUtil.getOracleConnection(); con.setAutoCommit(false); ps1=con.prepareStatement("select bal from accounts where accno='?'"); ps1.setInt(1,da); //**************//LINE 12******************// ResultSet rs1=ps1.executeQuery(); if(rs1.next()) dab=rs1.getInt(1); else throw new InvalidAccountNumberException(da); danb=dab+amt; //updating destination account balance ps2=con.prepareStatement("update accounts set bal='?' where accno='?' "); ps2.setInt(1,danb); ps2.setInt(2,da); ps2.executeUpdate(); System.out.println("**"+da+"*updated*"); //checking source account ps1.setInt(1,sa); rs1=ps1.executeQuery(); if(rs1.next()){ sanb=rs1.getInt(1); }else{ throw new InvalidAccountNumberException(); } if(sab>=amt){ sanb=sab-amt; }else{ throw new InsufficientFundsException(); } //updating Source account ps2.setInt(1,sanb); ps2.setInt(2,sa); ps2.executeUpdate(); con.commit(); System.out.println("**"+sa+"updated"); System.out.println("funds transferred"); }catch(Exception e){ e.printStackTrace(); try{ con.rollback(); }catch(Exception e1){ } }finally{ JDBCUtil.cleanup(ps1, con); JDBCUtil.c

          L 1 Reply Last reply
          0
          • L LOKENDRA YADAV

            -----------------------------------DATABASE------------------------------------------------------------- CREATE TABLE ACCOUNTS(ACCNO INT PRIMARY KEY,BAL PRIMARY KEY); insert into accounts values(77,10000); insert into accounts values(88,10000); ---------------------------------HERE IS JDBC CODE---------------------------------------------------- import A.JDBCUtil; // ****JDBCUtil is the class having connection code import java.sql.*; public class Test12 { public static void main(String[] args) { Account acc=new Account(); acc.transfer(77,88,5000); //******LINE 8****** } } class InvalidAccountNumberException extends Exception{ int accno; public InvalidAccountNumberException() { } public InvalidAccountNumberException(int accno){ this.accno=accno; } @Override public String toString() { return "Accno"+accno+"not found"; } } class InsufficientFundsException extends Exception{ public InsufficientFundsException() { } @Override public String toString() { return "sufficent funds are not available"; } } class Account{ int bal; int sab,dab,danb,sanb; public void transfer(int sa,int da,int amt){ Connection con=null; PreparedStatement ps1=null; PreparedStatement ps2=null; try{ con=JDBCUtil.getOracleConnection(); con.setAutoCommit(false); ps1=con.prepareStatement("select bal from accounts where accno='?'"); ps1.setInt(1,da); //**************//LINE 12******************// ResultSet rs1=ps1.executeQuery(); if(rs1.next()) dab=rs1.getInt(1); else throw new InvalidAccountNumberException(da); danb=dab+amt; //updating destination account balance ps2=con.prepareStatement("update accounts set bal='?' where accno='?' "); ps2.setInt(1,danb); ps2.setInt(2,da); ps2.executeUpdate(); System.out.println("**"+da+"*updated*"); //checking source account ps1.setInt(1,sa); rs1=ps1.executeQuery(); if(rs1.next()){ sanb=rs1.getInt(1); }else{ throw new InvalidAccountNumberException(); } if(sab>=amt){ sanb=sab-amt; }else{ throw new InsufficientFundsException(); } //updating Source account ps2.setInt(1,sanb); ps2.setInt(2,sa); ps2.executeUpdate(); con.commit(); System.out.println("**"+sa+"updated"); System.out.println("funds transferred"); }catch(Exception e){ e.printStackTrace(); try{ con.rollback(); }catch(Exception e1){ } }finally{ JDBCUtil.cleanup(ps1, con); JDBCUtil.c

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I cannot see anything wrong with your parameter indices, unless something else is corrupting those values. You need to use your debugger to step through the code and see exactly what is happening.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups