SQL Query In Java + Oracle












0














I'm really struggling to get this SQL into my head while using java.



My problem is: I want to use a variable in my sql Query, and i cant seem to get it working, it catches the correct value(i'm showing it on a label), But it doesnt show any records, yet, if i replace the variable for a '5', it shows me the correct record...



        try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = MySqlConnect.ConnectDb();


int idaca = Integer.parseInt(idhist.getText());
String query1 = "SELECT t.nome, h.Valor_Atual, h.Valor_Antigo, a.nome
FROM Tecnologias t, Historico h, Academista a
WHERE h.Id_Academista = a.Id_Academista AND a.Id_Academista = "+idaca+" AND h.Id_Tecnologia = t.Id_Tecnologia
AND (h.Valor_Atual || h.Valor_Antigo || t.nome) LIKE '%" + ValToSearch + "%'";

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query1);

historico history;

while (rs.next()) {
history = new historico(rs.getString("Nome"), rs.getInt("Valor_Antigo"),
rs.getInt("Valor_Atual"), rs.getString("Nome"));
historicoList.add(history);
} //END WHILE
} //END TRY
catch (Exception e) {
JOptionPane.showInputDialog(null, e);
}//END CATCH


Thats my code so far... The ValToSearch is working fine, tho...



Thank you in advance! Cheers










share|improve this question




















  • 1




    The ValToSearch is working fine, tho - If your query works when you manually type '%5%', it means that ValToSearch is not working. However the issue might be elsewhere in your code, it's hard to tell with just two lines of code
    – BackSlash
    Nov 22 at 11:41












  • Im having problems with "+idaca+", if i replace it with 5, it works fine.. :/
    – Eduardo Fernandes
    Nov 22 at 11:42










  • please use some query builder eg stackoverflow.com/questions/5620985/…
    – bato3
    Nov 22 at 11:42










  • add space before this AND idaca+"AND
    – bato3
    Nov 22 at 11:43






  • 1




    @EduardoFernandes Try to print query1 by adding System.out.println("sql query --> "+query1);
    – Jåcob
    Nov 22 at 11:48


















0














I'm really struggling to get this SQL into my head while using java.



My problem is: I want to use a variable in my sql Query, and i cant seem to get it working, it catches the correct value(i'm showing it on a label), But it doesnt show any records, yet, if i replace the variable for a '5', it shows me the correct record...



        try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = MySqlConnect.ConnectDb();


int idaca = Integer.parseInt(idhist.getText());
String query1 = "SELECT t.nome, h.Valor_Atual, h.Valor_Antigo, a.nome
FROM Tecnologias t, Historico h, Academista a
WHERE h.Id_Academista = a.Id_Academista AND a.Id_Academista = "+idaca+" AND h.Id_Tecnologia = t.Id_Tecnologia
AND (h.Valor_Atual || h.Valor_Antigo || t.nome) LIKE '%" + ValToSearch + "%'";

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query1);

historico history;

while (rs.next()) {
history = new historico(rs.getString("Nome"), rs.getInt("Valor_Antigo"),
rs.getInt("Valor_Atual"), rs.getString("Nome"));
historicoList.add(history);
} //END WHILE
} //END TRY
catch (Exception e) {
JOptionPane.showInputDialog(null, e);
}//END CATCH


Thats my code so far... The ValToSearch is working fine, tho...



Thank you in advance! Cheers










share|improve this question




















  • 1




    The ValToSearch is working fine, tho - If your query works when you manually type '%5%', it means that ValToSearch is not working. However the issue might be elsewhere in your code, it's hard to tell with just two lines of code
    – BackSlash
    Nov 22 at 11:41












  • Im having problems with "+idaca+", if i replace it with 5, it works fine.. :/
    – Eduardo Fernandes
    Nov 22 at 11:42










  • please use some query builder eg stackoverflow.com/questions/5620985/…
    – bato3
    Nov 22 at 11:42










  • add space before this AND idaca+"AND
    – bato3
    Nov 22 at 11:43






  • 1




    @EduardoFernandes Try to print query1 by adding System.out.println("sql query --> "+query1);
    – Jåcob
    Nov 22 at 11:48
















0












0








0







I'm really struggling to get this SQL into my head while using java.



My problem is: I want to use a variable in my sql Query, and i cant seem to get it working, it catches the correct value(i'm showing it on a label), But it doesnt show any records, yet, if i replace the variable for a '5', it shows me the correct record...



        try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = MySqlConnect.ConnectDb();


int idaca = Integer.parseInt(idhist.getText());
String query1 = "SELECT t.nome, h.Valor_Atual, h.Valor_Antigo, a.nome
FROM Tecnologias t, Historico h, Academista a
WHERE h.Id_Academista = a.Id_Academista AND a.Id_Academista = "+idaca+" AND h.Id_Tecnologia = t.Id_Tecnologia
AND (h.Valor_Atual || h.Valor_Antigo || t.nome) LIKE '%" + ValToSearch + "%'";

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query1);

historico history;

while (rs.next()) {
history = new historico(rs.getString("Nome"), rs.getInt("Valor_Antigo"),
rs.getInt("Valor_Atual"), rs.getString("Nome"));
historicoList.add(history);
} //END WHILE
} //END TRY
catch (Exception e) {
JOptionPane.showInputDialog(null, e);
}//END CATCH


Thats my code so far... The ValToSearch is working fine, tho...



Thank you in advance! Cheers










share|improve this question















I'm really struggling to get this SQL into my head while using java.



My problem is: I want to use a variable in my sql Query, and i cant seem to get it working, it catches the correct value(i'm showing it on a label), But it doesnt show any records, yet, if i replace the variable for a '5', it shows me the correct record...



        try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = MySqlConnect.ConnectDb();


int idaca = Integer.parseInt(idhist.getText());
String query1 = "SELECT t.nome, h.Valor_Atual, h.Valor_Antigo, a.nome
FROM Tecnologias t, Historico h, Academista a
WHERE h.Id_Academista = a.Id_Academista AND a.Id_Academista = "+idaca+" AND h.Id_Tecnologia = t.Id_Tecnologia
AND (h.Valor_Atual || h.Valor_Antigo || t.nome) LIKE '%" + ValToSearch + "%'";

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query1);

historico history;

while (rs.next()) {
history = new historico(rs.getString("Nome"), rs.getInt("Valor_Antigo"),
rs.getInt("Valor_Atual"), rs.getString("Nome"));
historicoList.add(history);
} //END WHILE
} //END TRY
catch (Exception e) {
JOptionPane.showInputDialog(null, e);
}//END CATCH


Thats my code so far... The ValToSearch is working fine, tho...



Thank you in advance! Cheers







java mysql sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 12:13









MT0

51.8k52756




51.8k52756










asked Nov 22 at 11:38









Eduardo Fernandes

34




34








  • 1




    The ValToSearch is working fine, tho - If your query works when you manually type '%5%', it means that ValToSearch is not working. However the issue might be elsewhere in your code, it's hard to tell with just two lines of code
    – BackSlash
    Nov 22 at 11:41












  • Im having problems with "+idaca+", if i replace it with 5, it works fine.. :/
    – Eduardo Fernandes
    Nov 22 at 11:42










  • please use some query builder eg stackoverflow.com/questions/5620985/…
    – bato3
    Nov 22 at 11:42










  • add space before this AND idaca+"AND
    – bato3
    Nov 22 at 11:43






  • 1




    @EduardoFernandes Try to print query1 by adding System.out.println("sql query --> "+query1);
    – Jåcob
    Nov 22 at 11:48
















  • 1




    The ValToSearch is working fine, tho - If your query works when you manually type '%5%', it means that ValToSearch is not working. However the issue might be elsewhere in your code, it's hard to tell with just two lines of code
    – BackSlash
    Nov 22 at 11:41












  • Im having problems with "+idaca+", if i replace it with 5, it works fine.. :/
    – Eduardo Fernandes
    Nov 22 at 11:42










  • please use some query builder eg stackoverflow.com/questions/5620985/…
    – bato3
    Nov 22 at 11:42










  • add space before this AND idaca+"AND
    – bato3
    Nov 22 at 11:43






  • 1




    @EduardoFernandes Try to print query1 by adding System.out.println("sql query --> "+query1);
    – Jåcob
    Nov 22 at 11:48










1




1




The ValToSearch is working fine, tho - If your query works when you manually type '%5%', it means that ValToSearch is not working. However the issue might be elsewhere in your code, it's hard to tell with just two lines of code
– BackSlash
Nov 22 at 11:41






The ValToSearch is working fine, tho - If your query works when you manually type '%5%', it means that ValToSearch is not working. However the issue might be elsewhere in your code, it's hard to tell with just two lines of code
– BackSlash
Nov 22 at 11:41














Im having problems with "+idaca+", if i replace it with 5, it works fine.. :/
– Eduardo Fernandes
Nov 22 at 11:42




Im having problems with "+idaca+", if i replace it with 5, it works fine.. :/
– Eduardo Fernandes
Nov 22 at 11:42












please use some query builder eg stackoverflow.com/questions/5620985/…
– bato3
Nov 22 at 11:42




please use some query builder eg stackoverflow.com/questions/5620985/…
– bato3
Nov 22 at 11:42












add space before this AND idaca+"AND
– bato3
Nov 22 at 11:43




add space before this AND idaca+"AND
– bato3
Nov 22 at 11:43




1




1




@EduardoFernandes Try to print query1 by adding System.out.println("sql query --> "+query1);
– Jåcob
Nov 22 at 11:48






@EduardoFernandes Try to print query1 by adding System.out.println("sql query --> "+query1);
– Jåcob
Nov 22 at 11:48














2 Answers
2






active

oldest

votes


















4














Put an space before AND h.Id_Tecnologia. That should solve your problem.






share|improve this answer

















  • 2




    The error message you get from the database would have probably pointed at that problem. Always look at error messages. Make sure your program prints them (not just try/catch/ignore). Make sure to include them in Stackoverflow questions.
    – Thilo
    Nov 22 at 11:44












  • There is no error message, thats the problem, it doesnt return anything
    – Eduardo Fernandes
    Nov 22 at 11:46










  • Edited answer, but still no results :/
    – Eduardo Fernandes
    Nov 22 at 11:49










  • do you use error handling like this docs.oracle.com/javase/tutorial/jdbc/basics/sqlexception.html
    – bato3
    Nov 22 at 11:50










  • I do, updated the code
    – Eduardo Fernandes
    Nov 22 at 12:12



















0














You are not afraid that in ValToSearch you get something like ' OR 1 IN (DELETE * FROM Tecnologias )?
Use parametr escaping or better some query builder






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430159%2fsql-query-in-java-oracle%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    Put an space before AND h.Id_Tecnologia. That should solve your problem.






    share|improve this answer

















    • 2




      The error message you get from the database would have probably pointed at that problem. Always look at error messages. Make sure your program prints them (not just try/catch/ignore). Make sure to include them in Stackoverflow questions.
      – Thilo
      Nov 22 at 11:44












    • There is no error message, thats the problem, it doesnt return anything
      – Eduardo Fernandes
      Nov 22 at 11:46










    • Edited answer, but still no results :/
      – Eduardo Fernandes
      Nov 22 at 11:49










    • do you use error handling like this docs.oracle.com/javase/tutorial/jdbc/basics/sqlexception.html
      – bato3
      Nov 22 at 11:50










    • I do, updated the code
      – Eduardo Fernandes
      Nov 22 at 12:12
















    4














    Put an space before AND h.Id_Tecnologia. That should solve your problem.






    share|improve this answer

















    • 2




      The error message you get from the database would have probably pointed at that problem. Always look at error messages. Make sure your program prints them (not just try/catch/ignore). Make sure to include them in Stackoverflow questions.
      – Thilo
      Nov 22 at 11:44












    • There is no error message, thats the problem, it doesnt return anything
      – Eduardo Fernandes
      Nov 22 at 11:46










    • Edited answer, but still no results :/
      – Eduardo Fernandes
      Nov 22 at 11:49










    • do you use error handling like this docs.oracle.com/javase/tutorial/jdbc/basics/sqlexception.html
      – bato3
      Nov 22 at 11:50










    • I do, updated the code
      – Eduardo Fernandes
      Nov 22 at 12:12














    4












    4








    4






    Put an space before AND h.Id_Tecnologia. That should solve your problem.






    share|improve this answer












    Put an space before AND h.Id_Tecnologia. That should solve your problem.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 at 11:42









    Rafael Palomino

    332213




    332213








    • 2




      The error message you get from the database would have probably pointed at that problem. Always look at error messages. Make sure your program prints them (not just try/catch/ignore). Make sure to include them in Stackoverflow questions.
      – Thilo
      Nov 22 at 11:44












    • There is no error message, thats the problem, it doesnt return anything
      – Eduardo Fernandes
      Nov 22 at 11:46










    • Edited answer, but still no results :/
      – Eduardo Fernandes
      Nov 22 at 11:49










    • do you use error handling like this docs.oracle.com/javase/tutorial/jdbc/basics/sqlexception.html
      – bato3
      Nov 22 at 11:50










    • I do, updated the code
      – Eduardo Fernandes
      Nov 22 at 12:12














    • 2




      The error message you get from the database would have probably pointed at that problem. Always look at error messages. Make sure your program prints them (not just try/catch/ignore). Make sure to include them in Stackoverflow questions.
      – Thilo
      Nov 22 at 11:44












    • There is no error message, thats the problem, it doesnt return anything
      – Eduardo Fernandes
      Nov 22 at 11:46










    • Edited answer, but still no results :/
      – Eduardo Fernandes
      Nov 22 at 11:49










    • do you use error handling like this docs.oracle.com/javase/tutorial/jdbc/basics/sqlexception.html
      – bato3
      Nov 22 at 11:50










    • I do, updated the code
      – Eduardo Fernandes
      Nov 22 at 12:12








    2




    2




    The error message you get from the database would have probably pointed at that problem. Always look at error messages. Make sure your program prints them (not just try/catch/ignore). Make sure to include them in Stackoverflow questions.
    – Thilo
    Nov 22 at 11:44






    The error message you get from the database would have probably pointed at that problem. Always look at error messages. Make sure your program prints them (not just try/catch/ignore). Make sure to include them in Stackoverflow questions.
    – Thilo
    Nov 22 at 11:44














    There is no error message, thats the problem, it doesnt return anything
    – Eduardo Fernandes
    Nov 22 at 11:46




    There is no error message, thats the problem, it doesnt return anything
    – Eduardo Fernandes
    Nov 22 at 11:46












    Edited answer, but still no results :/
    – Eduardo Fernandes
    Nov 22 at 11:49




    Edited answer, but still no results :/
    – Eduardo Fernandes
    Nov 22 at 11:49












    do you use error handling like this docs.oracle.com/javase/tutorial/jdbc/basics/sqlexception.html
    – bato3
    Nov 22 at 11:50




    do you use error handling like this docs.oracle.com/javase/tutorial/jdbc/basics/sqlexception.html
    – bato3
    Nov 22 at 11:50












    I do, updated the code
    – Eduardo Fernandes
    Nov 22 at 12:12




    I do, updated the code
    – Eduardo Fernandes
    Nov 22 at 12:12













    0














    You are not afraid that in ValToSearch you get something like ' OR 1 IN (DELETE * FROM Tecnologias )?
    Use parametr escaping or better some query builder






    share|improve this answer


























      0














      You are not afraid that in ValToSearch you get something like ' OR 1 IN (DELETE * FROM Tecnologias )?
      Use parametr escaping or better some query builder






      share|improve this answer
























        0












        0








        0






        You are not afraid that in ValToSearch you get something like ' OR 1 IN (DELETE * FROM Tecnologias )?
        Use parametr escaping or better some query builder






        share|improve this answer












        You are not afraid that in ValToSearch you get something like ' OR 1 IN (DELETE * FROM Tecnologias )?
        Use parametr escaping or better some query builder







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 11:48









        bato3

        865617




        865617






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430159%2fsql-query-in-java-oracle%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Fiat S.p.A.

            Type 'String' is not a subtype of type 'int' of 'index'