?
Solved

JDBC SELECT Statement Error

Posted on 2007-10-16
3
Medium Priority
?
645 Views
Last Modified: 2010-05-18
Howdy

I have a JDBC connection that allows me to create and delete tables, as well as insert into these tables.  However, I find myself in a position where I need to use a select statement and JDBC doesn't seem keen on allowing this, while being incredibly vague as to why.  Here's the code:

import java.sql.*;
import java.io.*;
import java.util.*;

public class RandomExtract
{
      public static void main(String[] args) throws Exception
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = DriverManager.getConnection("jdbc:mysql://jerry.phy.wfu.edu:3306/GENES?user=keydet98&password=p00rM1c3");
            Statement stmt = con.createStatement();
            ResultSet result = stmt.executeQuery("SELECT COUNT(*) FROM RefSeq");
            
            int Total = 0;                
                System.out.println(result.getInt(1));
        }
}

Here the error:

Exception in thread "main" java.sql.SQLException: Before start of result set

What am I missing above that needs to be done for a SELECT statement?

Jason


0
Comment
Question by:cdromes
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 20089290
           ResultSet result = stmt.executeQuery("SELECT COUNT(*) FROM RefSeq");
            int Total = 0;                
            if (result.next()) System.out.println(result.getInt(1));
0
 
LVL 9

Expert Comment

by:sanjooz
ID: 20089321
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

public class RandomExtract {
      public static void main(String[] args) throws Exception {
            String url = "jdbc:mysql://jerry.phy.wfu.edu:3306/GENES";
        String user = "keydet98";
        String password = "p00rM1c3";
        String driver = "com.mysql.jdbc.Driver";
        Connection connection;
       
        Class.forName(driver);
        connection = DriverManager.getConnection(url, user, password);


        Statement stmt = connection.createStatement();
        ResultSet result = stmt.executeQuery("SELECT COUNT(*) AS COUNT FROM RefSeq");
       
        int Total = 0;                
        System.out.println(result.getInt("COUNT"));
      }

}
0
 
LVL 9

Assisted Solution

by:sanjooz
sanjooz earned 100 total points
ID: 20089331
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

public class RandomExtract {
      public static void main(String[] args) throws Exception {
            String url = "jdbc:mysql://jerry.phy.wfu.edu:3306/GENES";
        String user = "keydet98";
        String password = "p00rM1c3";
        String driver = "com.mysql.jdbc.Driver";
        Connection connection;
       
        Class.forName(driver);
        connection = DriverManager.getConnection(url, user, password);


        Statement stmt = connection.createStatement();
        ResultSet result = stmt.executeQuery("SELECT COUNT(*) AS COUNT FROM RefSeq");
       
        int Total = 0;                
        result.next();
        System.out.println(result.getInt("COUNT"));
      }

}
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question