-3

When i run a query with mysqlconnector and fetching the result with ResulSet my first is always with an null before.

This my code:

Class.forName("com.mysql.jdbc.Driver");
    conn =DriverManager.getConnection("jdbc:mysql://localhost/","","");
    String query = "SELECT Category,Account FROM Settings" ;//
    PreparedStatement select = conn.prepareStatement(query);
    ResultSet rs = select.executeQuery( );

    while (rs.next())
        {

                category += rs.getString("Category")+",";
                account += rs.getString("Account")+",";

And my result is always: nullresult, result, result...

Even when its only one result it is always: nullResult

How can i fix this?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

3 Answers3

3

you may have declared null

My assumption is you have String category =null;

change is to String category ="";

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0

Variable that you have declared String category is either automatically or explicitly (in code) assigned as null.

So, Better to assign you variable category with ""(Blank String). that will gives you a result what you are expecting..

Sample Code Here is a Demo To Demonstrate Your Problem

String category = "";
categorty += "Appeltants Ronald";
System.out.prinln(category); //it will print Appeltants Ronald

Thank YOu.

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
-2

In the statement conn =DriverManager.getConnection("jdbc:mysql://localhost/","",""); you have not mentioned the name of the database and the username,password to access the database.

That's why there is no any connection made with the database and hence it is returning null.

asad_hussain
  • 1,959
  • 1
  • 17
  • 27