Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Im using SQL server and Netbeans and it gave me this error:

com.microsoft.sqlserver.jdbc.SQLServerException: The column name Max(CarNumber) is not valid.

from :
Java
Statement s = con.createStatement();
           
      ResultSet rs = s.executeQuery("select Max(CarNumber) from CarRegistration");
            rs.next();
            rs.getString("Max(CarNumber)");

            if (rs.getString("Max(CarNumber)")== null) 
            {
                txtregno.setText("C0001");
            } 
            else 
            {
              long id = Long.parseLong(rs.getString("Max(CarNumber)").substring(2,rs.getString("Max(CarNumber)").length()));
               id++;
             txtregno.setText("C0" + String.format("%03d", id));
            }


What I have tried:

I tried everything still getting the error. is anything wrong with the syntax?
Posted
Updated 8-Jan-20 8:07am
v2
Comments
Kris Lantz 8-Jan-20 13:35pm    
Does the column "CarNumber" exist in the DB?
Kurt Jimenez 8-Jan-20 13:40pm    
yes, im using microsoft sql server

Since you're using an aggregate you need to give an alias to the column if you want to reference it by name. For example
Statement s = con.createStatement();

ResultSet rs = s.executeQuery("select Max(CarNumber) AS MaxCarNo from CarRegistration");
rs.next();
rs.getString("MaxCarNo");

if (rs.getString("MaxCarNo")== null) {
...
 
Share this answer
 
Comments
Kris Lantz 8-Jan-20 13:58pm    
Beat me to it. Take my stars...
Wendelius 8-Jan-20 23:14pm    
Thank you.
Kurt Jimenez 8-Jan-20 14:12pm    
Yeah you're right bruhhh. Thank you :) and God bless you O:) you saved my day...
Wendelius 8-Jan-20 23:14pm    
Glad to be of service :)
That's probably because it is not a string value, try something like this:
int max = rs.getInt(1);

I'm not a Java programmer, so the syntax might not be correct :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900