2016-11-12 65 views
0

下面顯示嘗試運行我的代碼java.lang.ClassCastException:com.mysql.jdbc.JDBC4ResultSet不能轉換到com.mysql.jdbc.ResultSet

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

我不,當我收到一條錯誤信息不明白爲什麼會出現這個錯誤,所以有人可以給我解釋爲什麼甚至更好的解決問題的原因。下面是整個代碼

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

嘗試'進口java.sql.ResultSet中;',而不是'進口com.mysql.jdbc.ResultSet;'。 –

+2

並且不要轉換,您不需要MySQL類,只需使用'java.sql'接口即可。 –

回答

0

你必須改變你的包: - 這些都不是正確的:

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

您可以使用此....

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

,因爲你是使用連接作爲com.mysql和使用java.sql中像分別DriverManager的。這就是發生錯誤的原因。

相關問題