Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9064919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:22:32+00:00 2026-06-16T16:22:32+00:00

I have been trying to write a Java program which connects to mysql db

  • 0

I have been trying to write a Java program which connects to mysql db and retreives data from it.

I have written a simple program which worked successfully, but the below snippet throws some errors which I even havent heard of.

The snippet is as follows:

public class test{
String rows,rows1;
String dbUrl = "jdbc:mysql://localhost:3306/login";
String dbClass = "com.mysql.jdbc.Driver";
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection (dbUrl,"root","");
Statement stmt = con.createStatement();

String query="select distinct(username) from tracking";
ResultSet rs=stmt.executeQuery(query);
while(rs.next())
{
rows=rs.getString("username");
System.out.println(rows);
String query1="select distinct(session_id) from tracking where username='"+rows+"'";
ResultSet rs1=stmt.executeQuery(query1);
while(rs1.next())
{
rows1=rs1.getString("session_id");
String query2="select * from tracking where username='"+rows+"' and session_id='"+rows1+"'";
ResultSet rs2=stmt.executeQuery(query2);
while(rs2.next())
{
System.out.println("Result"+rs2.getString("method"));
}
}
}
con.close();
}
catch(ClassNotFoundException e) {
e.printStackTrace();
}

catch(SQLException e) {
e.printStackTrace();
}
};

The errors are as follows

test.java:12: error: non-static variable dbUrl cannot be referenced from a stati
c context
Connection con = DriverManager.getConnection (dbUrl,"root","");
                                              ^
test.java:19: error: non-static variable rows cannot be referenced from a static
 context
rows=rs.getString("username");
^
test.java:20: error: non-static variable rows cannot be referenced from a static
 context
System.out.println(rows);
                   ^
test.java:21: error: non-static variable rows cannot be referenced from a static
 context
String query1="select distinct(session_id) from tracking where username='"+rows+
"'";
                                                                           ^
test.java:25: error: non-static variable rows1 cannot be referenced from a stati
c context
rows1=rs1.getString("session_id");
^
test.java:26: error: non-static variable rows cannot be referenced from a static
 context
String query2="select * from tracking where username='"+rows+"' and session_id='
"+rows1+"'";
                                                        ^
test.java:26: error: non-static variable rows1 cannot be referenced from a stati
c context
String query2="select * from tracking where username='"+rows+"' and session_id='
"+rows1+"'";

I am unable to understand the errors here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:22:33+00:00Added an answer on June 16, 2026 at 4:22 pm

    Make your variables STATIC.

    static String rows,rows1;
    static String dbUrl = "jdbc:mysql://localhost:3306/login";
    static String dbClass = "com.mysql.jdbc.Driver";
    

    You are using the variables rows, rows1 etc in your MAIN method which is static. Whenever you are using any variables inside a static method, the variables itself has to be static.

    Probably you are making one of the common errors in Java. May be you can go through other types of common errors so that you can avoid them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have been trying to write a java program, that involves Threads. It
i have been trying to write a program in python to read two text
I have a Bug at my simple REGEX. I have been trying to write
i have been trying to write some programs on java sockets and i got
I have a database and a Java Program. I am trying to write a
I have been trying to write some Java bytecode and assemble it using Jasmin.
In trying to write more testable Java code, I have been using the Model-View-Presenter
I have been trying to run simple map-reduce jobs on data stored in Cassandra
I program in Java and have been trying to understand exactly what operator overloading
I'm new to java and trying to write a simple program using JDBC. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.