java.lang.ClassCastException: com.mysql.jdbc.JDBC4

2019-09-11 15:45发布

问题:

Below shows an error message I am receiving when trying to run my code

java.lang.ClassCastException: com.mysql.jdbc.JDBC4ResultSet cannot be cast to com.mysql.jdbc.ResultSet

I don't understand why this error is occurring so could someone give me the reason why or even better give me a fix to the problem. Below is the entire code

package com.hotel.database;


import com.hotel.beans.VehicleTypeBean;


import java.sql.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Vector;
import com.hotel.beans.ReservationDetailsBean;
import com.hotel.beans.AgentBean;
import com.hotel.beans.CheckInBean;
import com.hotel.beans.CompanyBean;
import com.hotel.beans.CustomerBean;
import com.hotel.beans.RecieptBean;
import com.hotel.beans.ReservationBean;
import com.hotel.beans.RoomClassBean;
import com.hotel.beans.RoomRateBean;
import com.hotel.beans.RoomStatusBean;
import com.hotel.beans.TransportRecieptBean;
import com.hotel.beans.TransportfareBean;
import com.hotel.beans.VehicleBean;
import com.hotel.beans.VehicleDriverBean;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Vector;
/**
 *
 * @author lenovo
 */
public class Transport {
     Connection conn=null;
    ResultSet rs=null;
    PreparedStatement stmt=null;
    private Vector v;
    private LinkedList[] lst;
    private int stId;
    private Statement state;
    private ResultSet res;
    public Transport(){
         conn=ConnectionProvider.getConnection();
    }
public LinkedList [] getWalkInGuests()throws Exception
        {
           int count=0;
stmt=(PreparedStatement) conn.prepareStatement("select count(distinct customerId) from transport where reservationId=-1");
rs=(ResultSet) stmt.executeQuery();
    if(rs.next())
    {
    count=rs.getInt(1);
    }
    lst=new LinkedList[count];
        if(lst.length<1)
            return lst;
    count=0;
   stmt=(PreparedStatement) conn.prepareStatement("SELECT customer.customer_id,customer.firstName,customer.lastName,customer.address from customer where customer.customer_id IN (select customerId from transport where reservationid=-1)");
         // stmt.setInt(1, )
    rs=(ResultSet) stmt.executeQuery();
    while(rs.next())
    {
        lst[count]=new LinkedList();
        lst[count].add(rs.getInt("customer.customer_id"));
        lst[count].add(rs.getString("customer.firstName"));
        lst[count].add(rs.getString("customer.lastName"));
    lst[count].add(rs.getString("customer.address"));
     count++;
    }   
    return lst;
}
}

回答1:

You have to change your packages :- These are not right:

import com.mysql.jdbc.DriverManager;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

You can use this ....

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;

because you are using connection as a com.mysql and DriverManager using java.sql like respectively. That is why the errors occurred.



标签: java mysql jdbc