JDBC connection in Java with Eclipse ( when a method calling a method)










0















I'm trying to connect to my DB using JDBC. I wanted to make a method for connection and another method for selecting data. I am getting a red line in Eclipse on the 'Connection con = connectDB();' part. ( See also attached) Cany anyone give me advice?



public class DBJdbc {



 //Statement stmt = null;

// connecting to DB
public void connectDB()

//Connection con = null;

try
Class.forName("com.mysql.cj.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "***", "***");

catch(SQLException e)
e.printStackTrace();

catch(ClassNotFoundException e)
e.printStackTrace();





// a method for selecting DB
public static void select()

//connectDB();

String sql = "SELECT * from SAC_SUR";

try(Connection con = connectDB(); // I'm getting a red line here)
PreparedStatement pstmt = con.prepareStatement(sql))
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
int id = rs.getInt(1);
String name = rs.getString(2);
System.out.println("Id = " + id + "name = " + name);
//while
catch(SQLException e)
System.out.println(e.getMessage());




red line here!!!










share|improve this question






















  • Return type of connectDB() is void, meaning there is no return value, so what did you expect Connection con = connectDB() to assign to con?

    – Andreas
    Nov 13 '18 at 5:14











  • Why do you have two Statement objects, pstmt and stmt, with the same sql?

    – Andreas
    Nov 13 '18 at 5:16











  • Since you're using try-with-resources, you should also use it on the ResultSet object.

    – Andreas
    Nov 13 '18 at 5:17











  • @Andreas oops. thank you~

    – Jin Lee
    Nov 13 '18 at 5:18











  • @Andreas So instead of rs , I change it to 'con'?

    – Jin Lee
    Nov 13 '18 at 5:32















0















I'm trying to connect to my DB using JDBC. I wanted to make a method for connection and another method for selecting data. I am getting a red line in Eclipse on the 'Connection con = connectDB();' part. ( See also attached) Cany anyone give me advice?



public class DBJdbc {



 //Statement stmt = null;

// connecting to DB
public void connectDB()

//Connection con = null;

try
Class.forName("com.mysql.cj.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "***", "***");

catch(SQLException e)
e.printStackTrace();

catch(ClassNotFoundException e)
e.printStackTrace();





// a method for selecting DB
public static void select()

//connectDB();

String sql = "SELECT * from SAC_SUR";

try(Connection con = connectDB(); // I'm getting a red line here)
PreparedStatement pstmt = con.prepareStatement(sql))
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
int id = rs.getInt(1);
String name = rs.getString(2);
System.out.println("Id = " + id + "name = " + name);
//while
catch(SQLException e)
System.out.println(e.getMessage());




red line here!!!










share|improve this question






















  • Return type of connectDB() is void, meaning there is no return value, so what did you expect Connection con = connectDB() to assign to con?

    – Andreas
    Nov 13 '18 at 5:14











  • Why do you have two Statement objects, pstmt and stmt, with the same sql?

    – Andreas
    Nov 13 '18 at 5:16











  • Since you're using try-with-resources, you should also use it on the ResultSet object.

    – Andreas
    Nov 13 '18 at 5:17











  • @Andreas oops. thank you~

    – Jin Lee
    Nov 13 '18 at 5:18











  • @Andreas So instead of rs , I change it to 'con'?

    – Jin Lee
    Nov 13 '18 at 5:32













0












0








0








I'm trying to connect to my DB using JDBC. I wanted to make a method for connection and another method for selecting data. I am getting a red line in Eclipse on the 'Connection con = connectDB();' part. ( See also attached) Cany anyone give me advice?



public class DBJdbc {



 //Statement stmt = null;

// connecting to DB
public void connectDB()

//Connection con = null;

try
Class.forName("com.mysql.cj.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "***", "***");

catch(SQLException e)
e.printStackTrace();

catch(ClassNotFoundException e)
e.printStackTrace();





// a method for selecting DB
public static void select()

//connectDB();

String sql = "SELECT * from SAC_SUR";

try(Connection con = connectDB(); // I'm getting a red line here)
PreparedStatement pstmt = con.prepareStatement(sql))
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
int id = rs.getInt(1);
String name = rs.getString(2);
System.out.println("Id = " + id + "name = " + name);
//while
catch(SQLException e)
System.out.println(e.getMessage());




red line here!!!










share|improve this question














I'm trying to connect to my DB using JDBC. I wanted to make a method for connection and another method for selecting data. I am getting a red line in Eclipse on the 'Connection con = connectDB();' part. ( See also attached) Cany anyone give me advice?



public class DBJdbc {



 //Statement stmt = null;

// connecting to DB
public void connectDB()

//Connection con = null;

try
Class.forName("com.mysql.cj.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "***", "***");

catch(SQLException e)
e.printStackTrace();

catch(ClassNotFoundException e)
e.printStackTrace();





// a method for selecting DB
public static void select()

//connectDB();

String sql = "SELECT * from SAC_SUR";

try(Connection con = connectDB(); // I'm getting a red line here)
PreparedStatement pstmt = con.prepareStatement(sql))
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
int id = rs.getInt(1);
String name = rs.getString(2);
System.out.println("Id = " + id + "name = " + name);
//while
catch(SQLException e)
System.out.println(e.getMessage());




red line here!!!







java jdbc methods






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 5:06









Jin LeeJin Lee

14710




14710












  • Return type of connectDB() is void, meaning there is no return value, so what did you expect Connection con = connectDB() to assign to con?

    – Andreas
    Nov 13 '18 at 5:14











  • Why do you have two Statement objects, pstmt and stmt, with the same sql?

    – Andreas
    Nov 13 '18 at 5:16











  • Since you're using try-with-resources, you should also use it on the ResultSet object.

    – Andreas
    Nov 13 '18 at 5:17











  • @Andreas oops. thank you~

    – Jin Lee
    Nov 13 '18 at 5:18











  • @Andreas So instead of rs , I change it to 'con'?

    – Jin Lee
    Nov 13 '18 at 5:32

















  • Return type of connectDB() is void, meaning there is no return value, so what did you expect Connection con = connectDB() to assign to con?

    – Andreas
    Nov 13 '18 at 5:14











  • Why do you have two Statement objects, pstmt and stmt, with the same sql?

    – Andreas
    Nov 13 '18 at 5:16











  • Since you're using try-with-resources, you should also use it on the ResultSet object.

    – Andreas
    Nov 13 '18 at 5:17











  • @Andreas oops. thank you~

    – Jin Lee
    Nov 13 '18 at 5:18











  • @Andreas So instead of rs , I change it to 'con'?

    – Jin Lee
    Nov 13 '18 at 5:32
















Return type of connectDB() is void, meaning there is no return value, so what did you expect Connection con = connectDB() to assign to con?

– Andreas
Nov 13 '18 at 5:14





Return type of connectDB() is void, meaning there is no return value, so what did you expect Connection con = connectDB() to assign to con?

– Andreas
Nov 13 '18 at 5:14













Why do you have two Statement objects, pstmt and stmt, with the same sql?

– Andreas
Nov 13 '18 at 5:16





Why do you have two Statement objects, pstmt and stmt, with the same sql?

– Andreas
Nov 13 '18 at 5:16













Since you're using try-with-resources, you should also use it on the ResultSet object.

– Andreas
Nov 13 '18 at 5:17





Since you're using try-with-resources, you should also use it on the ResultSet object.

– Andreas
Nov 13 '18 at 5:17













@Andreas oops. thank you~

– Jin Lee
Nov 13 '18 at 5:18





@Andreas oops. thank you~

– Jin Lee
Nov 13 '18 at 5:18













@Andreas So instead of rs , I change it to 'con'?

– Jin Lee
Nov 13 '18 at 5:32





@Andreas So instead of rs , I change it to 'con'?

– Jin Lee
Nov 13 '18 at 5:32












2 Answers
2






active

oldest

votes


















1














connectDB() method is of void type and not returning anything but when you are calling the method, you are assigning it to variable con. So you need to change the return type of connectDb to the Connection type.



public Connection connectDB() 

Connection con = null;

try
Class.forName("com.mysql.cj.jdbc.Driver");

con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "***", "***");

catch(SQLException e)
e.printStackTrace();

catch(ClassNotFoundException e)
e.printStackTrace();


return con;






share|improve this answer























  • If either of those catch clauses are invoked, that code will lead the NullPointerException once code reached con.prepareStatement(sql)

    – Andreas
    Nov 13 '18 at 5:16











  • Accept or up vote it if it helped.

    – Pooja Aggarwal
    Nov 13 '18 at 5:22











  • @Andreas Andreas, I got NullPointerException like you said :( How can I fix this? Please give me more hint.

    – Jin Lee
    Nov 13 '18 at 5:24











  • @Pooja Aggarwal okay~~ I'm quite new to this. sorry.

    – Jin Lee
    Nov 13 '18 at 5:24











  • @JinLee Don't catch exception and continue as-if nothing went wrong. Basically, don't catch exception.

    – Andreas
    Nov 13 '18 at 5:25


















1














You are trying to call non-static method into the static area, which is not allowed in Java. So I made this method static and returning the database connection.



Please update the below method into your code. It will resolve your problem.



public static Connection connectDB() 
Connection con = null;
try
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "", "");
catch(SQLException e)
e.printStackTrace();
catch(ClassNotFoundException e)
e.printStackTrace();

return con;






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%2f53274162%2fjdbc-connection-in-java-with-eclipse-when-a-method-calling-a-method%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









    1














    connectDB() method is of void type and not returning anything but when you are calling the method, you are assigning it to variable con. So you need to change the return type of connectDb to the Connection type.



    public Connection connectDB() 

    Connection con = null;

    try
    Class.forName("com.mysql.cj.jdbc.Driver");

    con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "***", "***");

    catch(SQLException e)
    e.printStackTrace();

    catch(ClassNotFoundException e)
    e.printStackTrace();


    return con;






    share|improve this answer























    • If either of those catch clauses are invoked, that code will lead the NullPointerException once code reached con.prepareStatement(sql)

      – Andreas
      Nov 13 '18 at 5:16











    • Accept or up vote it if it helped.

      – Pooja Aggarwal
      Nov 13 '18 at 5:22











    • @Andreas Andreas, I got NullPointerException like you said :( How can I fix this? Please give me more hint.

      – Jin Lee
      Nov 13 '18 at 5:24











    • @Pooja Aggarwal okay~~ I'm quite new to this. sorry.

      – Jin Lee
      Nov 13 '18 at 5:24











    • @JinLee Don't catch exception and continue as-if nothing went wrong. Basically, don't catch exception.

      – Andreas
      Nov 13 '18 at 5:25















    1














    connectDB() method is of void type and not returning anything but when you are calling the method, you are assigning it to variable con. So you need to change the return type of connectDb to the Connection type.



    public Connection connectDB() 

    Connection con = null;

    try
    Class.forName("com.mysql.cj.jdbc.Driver");

    con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "***", "***");

    catch(SQLException e)
    e.printStackTrace();

    catch(ClassNotFoundException e)
    e.printStackTrace();


    return con;






    share|improve this answer























    • If either of those catch clauses are invoked, that code will lead the NullPointerException once code reached con.prepareStatement(sql)

      – Andreas
      Nov 13 '18 at 5:16











    • Accept or up vote it if it helped.

      – Pooja Aggarwal
      Nov 13 '18 at 5:22











    • @Andreas Andreas, I got NullPointerException like you said :( How can I fix this? Please give me more hint.

      – Jin Lee
      Nov 13 '18 at 5:24











    • @Pooja Aggarwal okay~~ I'm quite new to this. sorry.

      – Jin Lee
      Nov 13 '18 at 5:24











    • @JinLee Don't catch exception and continue as-if nothing went wrong. Basically, don't catch exception.

      – Andreas
      Nov 13 '18 at 5:25













    1












    1








    1







    connectDB() method is of void type and not returning anything but when you are calling the method, you are assigning it to variable con. So you need to change the return type of connectDb to the Connection type.



    public Connection connectDB() 

    Connection con = null;

    try
    Class.forName("com.mysql.cj.jdbc.Driver");

    con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "***", "***");

    catch(SQLException e)
    e.printStackTrace();

    catch(ClassNotFoundException e)
    e.printStackTrace();


    return con;






    share|improve this answer













    connectDB() method is of void type and not returning anything but when you are calling the method, you are assigning it to variable con. So you need to change the return type of connectDb to the Connection type.



    public Connection connectDB() 

    Connection con = null;

    try
    Class.forName("com.mysql.cj.jdbc.Driver");

    con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "***", "***");

    catch(SQLException e)
    e.printStackTrace();

    catch(ClassNotFoundException e)
    e.printStackTrace();


    return con;







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 5:09









    Pooja AggarwalPooja Aggarwal

    860111




    860111












    • If either of those catch clauses are invoked, that code will lead the NullPointerException once code reached con.prepareStatement(sql)

      – Andreas
      Nov 13 '18 at 5:16











    • Accept or up vote it if it helped.

      – Pooja Aggarwal
      Nov 13 '18 at 5:22











    • @Andreas Andreas, I got NullPointerException like you said :( How can I fix this? Please give me more hint.

      – Jin Lee
      Nov 13 '18 at 5:24











    • @Pooja Aggarwal okay~~ I'm quite new to this. sorry.

      – Jin Lee
      Nov 13 '18 at 5:24











    • @JinLee Don't catch exception and continue as-if nothing went wrong. Basically, don't catch exception.

      – Andreas
      Nov 13 '18 at 5:25

















    • If either of those catch clauses are invoked, that code will lead the NullPointerException once code reached con.prepareStatement(sql)

      – Andreas
      Nov 13 '18 at 5:16











    • Accept or up vote it if it helped.

      – Pooja Aggarwal
      Nov 13 '18 at 5:22











    • @Andreas Andreas, I got NullPointerException like you said :( How can I fix this? Please give me more hint.

      – Jin Lee
      Nov 13 '18 at 5:24











    • @Pooja Aggarwal okay~~ I'm quite new to this. sorry.

      – Jin Lee
      Nov 13 '18 at 5:24











    • @JinLee Don't catch exception and continue as-if nothing went wrong. Basically, don't catch exception.

      – Andreas
      Nov 13 '18 at 5:25
















    If either of those catch clauses are invoked, that code will lead the NullPointerException once code reached con.prepareStatement(sql)

    – Andreas
    Nov 13 '18 at 5:16





    If either of those catch clauses are invoked, that code will lead the NullPointerException once code reached con.prepareStatement(sql)

    – Andreas
    Nov 13 '18 at 5:16













    Accept or up vote it if it helped.

    – Pooja Aggarwal
    Nov 13 '18 at 5:22





    Accept or up vote it if it helped.

    – Pooja Aggarwal
    Nov 13 '18 at 5:22













    @Andreas Andreas, I got NullPointerException like you said :( How can I fix this? Please give me more hint.

    – Jin Lee
    Nov 13 '18 at 5:24





    @Andreas Andreas, I got NullPointerException like you said :( How can I fix this? Please give me more hint.

    – Jin Lee
    Nov 13 '18 at 5:24













    @Pooja Aggarwal okay~~ I'm quite new to this. sorry.

    – Jin Lee
    Nov 13 '18 at 5:24





    @Pooja Aggarwal okay~~ I'm quite new to this. sorry.

    – Jin Lee
    Nov 13 '18 at 5:24













    @JinLee Don't catch exception and continue as-if nothing went wrong. Basically, don't catch exception.

    – Andreas
    Nov 13 '18 at 5:25





    @JinLee Don't catch exception and continue as-if nothing went wrong. Basically, don't catch exception.

    – Andreas
    Nov 13 '18 at 5:25













    1














    You are trying to call non-static method into the static area, which is not allowed in Java. So I made this method static and returning the database connection.



    Please update the below method into your code. It will resolve your problem.



    public static Connection connectDB() 
    Connection con = null;
    try
    Class.forName("com.mysql.cj.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "", "");
    catch(SQLException e)
    e.printStackTrace();
    catch(ClassNotFoundException e)
    e.printStackTrace();

    return con;






    share|improve this answer





























      1














      You are trying to call non-static method into the static area, which is not allowed in Java. So I made this method static and returning the database connection.



      Please update the below method into your code. It will resolve your problem.



      public static Connection connectDB() 
      Connection con = null;
      try
      Class.forName("com.mysql.cj.jdbc.Driver");
      con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "", "");
      catch(SQLException e)
      e.printStackTrace();
      catch(ClassNotFoundException e)
      e.printStackTrace();

      return con;






      share|improve this answer



























        1












        1








        1







        You are trying to call non-static method into the static area, which is not allowed in Java. So I made this method static and returning the database connection.



        Please update the below method into your code. It will resolve your problem.



        public static Connection connectDB() 
        Connection con = null;
        try
        Class.forName("com.mysql.cj.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "", "");
        catch(SQLException e)
        e.printStackTrace();
        catch(ClassNotFoundException e)
        e.printStackTrace();

        return con;






        share|improve this answer















        You are trying to call non-static method into the static area, which is not allowed in Java. So I made this method static and returning the database connection.



        Please update the below method into your code. It will resolve your problem.



        public static Connection connectDB() 
        Connection con = null;
        try
        Class.forName("com.mysql.cj.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "", "");
        catch(SQLException e)
        e.printStackTrace();
        catch(ClassNotFoundException e)
        e.printStackTrace();

        return con;







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 6:11









        Michal

        9101021




        9101021










        answered Nov 13 '18 at 5:18









        user10644180user10644180

        111




        111



























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274162%2fjdbc-connection-in-java-with-eclipse-when-a-method-calling-a-method%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







            這個網誌中的熱門文章

            What does pagestruct do in Eviews?

            Dutch intervention in Lombok and Karangasem

            Channel Islands