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: How to insert a SUM with a SQL Sentence from Java in the same row

  1. #1
    Junior Member
    Join Date
    Sep 2022
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question How to insert a SUM with a SQL Sentence from Java in the same row

    Hi Everybody I'm New,

    I use this forum because i am creating a program where I can register a person and how much money debt, how much pay and its total.
    The idea is store it in a database.

    I have can not insert the subtraction of SUM(Deuda-Abono) in the colum total in the same row, the most I could do was generate the total but duplicating the line with this may be i am wrong

    INSERT INTO Clientes (Total)
    SELECT SUM(Deuda-Abono)
    From Clientes
    Group By ID;

    Here is how look my program
    [url=https://prnt.sc/78g8QA3IWgsi]


    This is my code when i run the boton Add, as you can see I dont insert the colum total so I think that I could do the operation and storage it in the same row with another sentence.

    private void btnAgregarActionPerformed(java.awt.event.ActionEve nt evt) {

    Conexion objConexion = new Conexion(); //Object and class conection

    Clientes oClientes = recuperarDatosGui(); //recuperarDatosGui - retrieve the information from the database

    String strSentenciaInsert = String.format("INSERT INTO Clientes(ID, Nombre, Telefono, Deuda, Abono)"
    + "VALUES (null, '%s', '%s', '%s','%s')", oClientes.getNombre(), oClientes.getTelefono(),oClientes.getDeuda(),oClie ntes.getAbono());
    //HACE REFERENCIA a ---------------------------
    objConexion.ejecutarSentenciaSQL(strSentenciaInser t);


    this.mostrarDatos();
    this.limpiarDatos();

    }



    Sentence to Substract:

    INSERT INTO Clientes (Total)
    SELECT SUM(Deuda-Abono)
    From Clientes
    Group By ID;




    I am new Programming so if you Could help me or if you can give another idea in how i can do my program I would appreciate it.

    Thanks.
    Last edited by Jose99; September 27th, 2022 at 01:29 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to insert a SUM with a SQL Sentence from Java in the same row

    Sorry, I do not know SQL. Try asking your question here: http://www.coderanch.com/forums

    Now posted here: https://coderanch.com/t/754648/java/...-Sentence-Java
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Jose99 (September 28th, 2022)

  4. #3
    Junior Member
    Join Date
    Sep 2022
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to insert a SUM with a SQL Sentence from Java in the same row

    Regards,

    Thank you for your answer I will post it there.

Similar Threads

  1. Replies: 1
    Last Post: July 11th, 2014, 12:48 PM
  2. Java B-Tree Insert (no libraries)
    By a1mz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2014, 06:01 AM
  3. Replies: 1
    Last Post: November 8th, 2013, 08:31 AM
  4. [SOLVED] Using Java CheckBox to insert data to MSAccess Database
    By sandlucky in forum JDBC & Databases
    Replies: 1
    Last Post: May 15th, 2011, 11:43 PM