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

  • SEARCH
  • Home
  • 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 3287124
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:27:28+00:00 2026-05-17T20:27:28+00:00

I am trying to connect to a MySQL database using Java. I want to

  • 0

I am trying to connect to a MySQL database using Java. I want to grab all the entries for two row from two rows in a database and put them into the class ExpertScore. I then want to put the newly created ExpertScore objects into an array of ExpertScore objects. After this I want to run them through a method that takes an array of ExpertScore objects as input. However, I run the code and get this error. Some debugging indicates that I think the problem results from the count of the result object.

java.sql.SQLException: Before start of result set
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
 at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)
 at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2672)
 at ZScoreCalc.main(ZScoreCalc.java:106)

This is my code that leads to the error:

public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
     System.out.println("MySQL Connect Example.");
     Connection conn = null;
     String url = "jdbc:mysql:*";    // server address
     String dbName = "QA";   //table name
     String driver = "com.mysql.jdbc.Driver"; // jdbc driver
     String userName = "*";   //username
     String password = "*";    //password
     Class.forName(driver).newInstance();
     try {
      conn = DriverManager.getConnection(url+dbName,userName,password);
      System.out.println("Connected to the database");

      for(int j=1; j<11; j++){
      String query = "select * from CONSUMER_EXPERT_SCORE where CONSUMER_EXPERT_ID="+j;
      String queryforcount = "select count(*) from CONSUMER_EXPERT_SCORE where CONSUMER_EXPERT_ID="+j;
      PreparedStatement pscount = conn.prepareStatement(queryforcount);
      ResultSet resultcount = pscount.executeQuery();
      int count = resultcount.getInt(0);
      PreparedStatement ps = conn.prepareStatement(query);
      ResultSet result = ps.executeQuery();
      int i=0;
      ExpertScore[] allUsers=new ExpertScore[count];
      while(result.next()){
       double expert=result.getDouble(3);
       int id=result.getInt(2);
       ExpertScore current=new ExpertScore(id, j, expert);
       allUsers[i]=current;
       i++;
      }
      ZScoreCalc scrCalc = new ZScoreCalc();
         scrCalc.Z_Calc(allUsers);
         scrCalc.print();
      }

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

}
}

Does anyone know what is going on here? Sorry I am new to programming and particularly new to JDBC.

  • 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-05-17T20:27:29+00:00Added an answer on May 17, 2026 at 8:27 pm

    You need to call resultcount.next() before calling resultcount.getInt(0);

    And it’s good practice to close your result sets in a finally clause. Here is generally the structure you would want to use.

    //in general use a structure like this:
    ResultSet rs = null;
    PreparedStatemetn pStmt = null;
    try {
    
     pStmt = conn.prepareStatement("Select * from foo");
     rs = pStmt.executeQuery();
     while (rs.next()) {
        String data = rs.getString(1);
     }
    }
    catch(Exception e){
     //handle exception
    }
    finally {
      try {
         if (rs != null) rs.close();
         if (pStmt != null) pStmt.close();
      }
      catch (Exception ignored) {
      }
    }
    

    }

    Pro Tip: For the common “close this resource an ignore exceptions” pattern, create utility methods:

    class DbUtil {
        public void closeQuietly(ResultSet rs) {
          try {
             if (rs != null) rs.close();
          }
          catch (Exception ignored) {}
        }
    
        public void closeQuietly(Statement stmt) {
          try {
             if (stmt != null) stmt.close();
          }
          catch (Exception ignored) {}
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to connect to MySQL database from node.js using db-mysql library. Everything works
I'm trying to connect to a MySQL 5 database using the MySQL ODBC 5.1
I am trying to connect to a remote mysql database from an iPhone. I
I'm trying to write a java program to connect to the same MySQL database
I'm trying to connect to remote MySQL server with SSL from PHP using mysql_connect:
I've been recently trying to connect to a hosted MySQL using Java but can't
I am trying to connect to a MySQL database using the mysql_connect() command however
I am trying to connect to MYSQL database using the Pear DB library. IS
I'm trying to connect to a MySQL database using my ASP.NET Web Forms Application.
I'm trying to connect to my MySQL database using Sequel-Pro and I'm kinda confused

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.