JDBC connection in Java with Eclipse ( when a method calling a method)
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
|
show 2 more comments
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
Return type ofconnectDB()isvoid, meaning there is no return value, so what did you expectConnection con = connectDB()to assign tocon?
– Andreas
Nov 13 '18 at 5:14
Why do you have two Statement objects,pstmtandstmt, with the samesql?
– Andreas
Nov 13 '18 at 5:16
Since you're using try-with-resources, you should also use it on theResultSetobject.
– 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
|
show 2 more comments
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
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
java jdbc methods
asked Nov 13 '18 at 5:06
Jin LeeJin Lee
14710
14710
Return type ofconnectDB()isvoid, meaning there is no return value, so what did you expectConnection con = connectDB()to assign tocon?
– Andreas
Nov 13 '18 at 5:14
Why do you have two Statement objects,pstmtandstmt, with the samesql?
– Andreas
Nov 13 '18 at 5:16
Since you're using try-with-resources, you should also use it on theResultSetobject.
– 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
|
show 2 more comments
Return type ofconnectDB()isvoid, meaning there is no return value, so what did you expectConnection con = connectDB()to assign tocon?
– Andreas
Nov 13 '18 at 5:14
Why do you have two Statement objects,pstmtandstmt, with the samesql?
– Andreas
Nov 13 '18 at 5:16
Since you're using try-with-resources, you should also use it on theResultSetobject.
– 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
|
show 2 more comments
2 Answers
2
active
oldest
votes
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;
If either of thosecatchclauses are invoked, that code will lead theNullPointerExceptiononce code reachedcon.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
|
show 1 more comment
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;
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
If either of thosecatchclauses are invoked, that code will lead theNullPointerExceptiononce code reachedcon.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
|
show 1 more comment
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;
If either of thosecatchclauses are invoked, that code will lead theNullPointerExceptiononce code reachedcon.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
|
show 1 more comment
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;
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;
answered Nov 13 '18 at 5:09
Pooja AggarwalPooja Aggarwal
860111
860111
If either of thosecatchclauses are invoked, that code will lead theNullPointerExceptiononce code reachedcon.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
|
show 1 more comment
If either of thosecatchclauses are invoked, that code will lead theNullPointerExceptiononce code reachedcon.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
|
show 1 more comment
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;
add a comment |
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;
add a comment |
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;
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;
edited Nov 13 '18 at 6:11
Michal
9101021
9101021
answered Nov 13 '18 at 5:18
user10644180user10644180
111
111
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Return type of
connectDB()isvoid, meaning there is no return value, so what did you expectConnection con = connectDB()to assign tocon?– Andreas
Nov 13 '18 at 5:14
Why do you have two Statement objects,
pstmtandstmt, with the samesql?– Andreas
Nov 13 '18 at 5:16
Since you're using try-with-resources, you should also use it on the
ResultSetobject.– 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