GVKun编程网logo

ClassCastException:无法将java.util.Date强制转换为java.sql.Date(无法将javac项识别为怎么改)

12

想了解ClassCastException:无法将java.util.Date强制转换为java.sql.Date的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于无法将javac项识别为怎么

想了解ClassCastException:无法将java.util.Date强制转换为java.sql.Date的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于无法将javac项识别为怎么改的相关问题,此外,我们还将为您介绍关于Android java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.EditText、ClassCastException:java.math.BigInteger在连接MySQL时无法强制转换为java.lang.Long、ClassCastException:不能在连接到MySQL时将java.math.BigInteger强制转换为java.lang.Long、ClassCastException:无法将java.lang.Object []强制转换为java.lang.String [] android的新知识。

本文目录一览:

ClassCastException:无法将java.util.Date强制转换为java.sql.Date(无法将javac项识别为怎么改)

ClassCastException:无法将java.util.Date强制转换为java.sql.Date(无法将javac项识别为怎么改)

您好,我的代码正在抛出ClassCastException。StackTrace显示:

java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date    at com.affiliate.DAO.AffiliateDAO.insertAffiliate(AffiliateDAO.java:48)

即@ ps.setDate(6,(Date)affiliate.getDate()); 在DAO

以下是我的servlet:

   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    Affiliate af= new Affiliate();    af.setFisrtName(request.getParameter("txtFname"));    af.setLastName(request.getParameter("txtLname"));    af.setGender(request.getParameter("txtGender"));    af.setCategory(request.getParameter("txtCategory"));    String dob=(request.getParameter("txtDob"));    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");      Date date;    try {        date = (Date)formatter.parse(dob);        af.setDate(date);    } catch (ParseException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }    af.setAge(Integer.parseInt(request.getParameter("txtAge")));    af.setAddress(request.getParameter("txtAddr"));    af.setCountry("India");    af.setState(request.getParameter("txtState"));    af.setCity(request.getParameter("txtCity"));    af.setPinCode(Integer.parseInt(request.getParameter("txtPin")));    af.setEmailId(request.getParameter("txtEmail"));    af.setStd(Integer.parseInt(request.getParameter("txtStd")));    af.setContactNo(Integer.parseInt(request.getParameter("txtPhone")));    af.setMobileNo(Long.parseLong(request.getParameter("txtMobile"),10));AffiliateService afs=new AffiliateService();**afs.createAffiliate(af);**}

以下是我的DAO:

public void insertAffiliate(Affiliate affiliate){    String sql="INSERT INTO REGISTER " +"(id,FisrtName,LastName,Gender,Category,DateOfBirth,Age,Address,Country,State,City,PinCode,EmailId,Std,ContactNo,MobileNo)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";    Connection conn = null;    try {        **conn = dataSource.createConnection();**        PreparedStatement ps = conn.prepareStatement(sql);        ps.setInt(1, affiliate.getId());        ps.setString(2, affiliate.getFisrtName());        ps.setString(3, affiliate.getLastName());        ps.setString(4,affiliate.getGender());        ps.setString(5, affiliate.getCategory());        ***ps.setDate(6, (Date) affiliate.getDate());***        ps.setInt(7, affiliate.getAge());        ps.setString(8, affiliate.getAddress());        ps.setString(9,affiliate.getCountry());        ps.setString(10,affiliate.getState());        ps.setString(11, affiliate.getCity());        ps.setInt(12, affiliate.getPinCode());        ps.setString(13, affiliate.getEmailId());        ps.setInt(14,affiliate.getStd());        ps.setInt(15, affiliate.getContactNo());        ps.setLong(16, affiliate.getMobileNo());        ps.executeUpdate();        ps.close();    } catch (SQLException e) {        throw new RuntimeException(e);    } finally {        if (conn != null) {            try {                conn.close();            } catch (SQLException e) {}        }    }}

以下是我的DTO:

public class Affiliate {@NotNull    @Past    Date date;public Date getDate() {        return date;    }    public void setDate(Date date) {        this.date = date;    }

请在这方面帮助我

答案1

小编典典

由于文档说,将Date在参数setDate()PreparedStatement需要类型的Date对象java.sql.Date。但是您似乎java.util.DateAffiliate课堂上使用了对象。

这就是为什么你得到了ClassCastException: java.util.Date cannot be cast tojava.sql.Date

要解决此问题,您需要将类中的Date对象类型更改Affiliatejava.sql.Date或执行此操作

ps.setDate(6, new java.sql.Date(affiliate.getDate().getTime()));

Android java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.EditText

Android java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.EditText

单击按钮将我从一个活动带到另一个活动时,我在Android中收到以下异常(我是Android开发中的新手,因此可能不是最聪明的问题):

java.lang.classCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText

我试过几次清理项目,尝试过Android Tools中的Fix Project Properties选项,已经检查了xml中的错误,但似乎无法弄清楚.我不得不提到,在发生此异常之前,该按钮可以正常工作一段时间.

这是我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/editCustomerPhone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_dark"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CustomerInfoActivity" >

<FrameLayout
    android:id="@+id/frameLayout1"
    android:layout_width="wrap_content"
    android:layout_height="250dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/ic_new_delivery" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Insert Customer Data:"
        android:textColor="@color/light_blue"
        android:textSize="20sp"
        android:text/>

</FrameLayout>

<EditText
    android:id="@+id/editCustomerName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_alignParentRight="true"
    android:ems="10"
    android:inputType="textPersonName"
    android:textColor="@color/light_blue" />

<Button
    android:id="@+id/submitInfoBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/editCustomerName"
    android:text="Submit Info"
    android:textColor="@color/light_red" 
    android:onClick="submitCustomerInfo"/>

<EditText
    android:id="@+id/editCustomerPhone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editCustomerName"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/TextView01"
    android:ems="10"
    android:inputType="phone"
    android:textColor="@color/light_red" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/frameLayout1"
    android:text="Name:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/background_light"
    android:textandroid:typeface="serif" />

<TextView
    android:id="@+id/TextView02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editCustomerPhone"
    android:layout_marginTop="14dp"
    android:text="Email:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/background_light"
    android:textandroid:typeface="serif" />

<EditText
    android:id="@+id/editCustomerEmail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/TextView02"
    android:layout_alignBottom="@+id/TextView02"
    android:layout_alignLeft="@+id/editCustomerPhone"
    android:layout_alignParentRight="true"
    android:ems="10"
    android:inputType="textEmailAddress"
    android:textColor="@color/light_green" />

<TextView
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/TextView02"
    android:layout_below="@+id/editCustomerName"
    android:layout_marginTop="19dp"
    android:text="Phone:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/background_light"
    android:textandroid:typeface="serif" />
@H_301_17@

这是我在活动中引发异常的onCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_customer_info);

    this.editMail = (EditText)findViewById(R.id.editCustomerEmail);
    this.editName = (EditText)findViewById(R.id.editCustomerName);
    this.editPhone = (EditText)findViewById(R.id.editCustomerPhone); // the exception points me here
}
@H_301_17@

解决方法:

您的代码说:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/editCustomerPhone"
@H_301_17@

editCustomerPhone是相对布局.

ClassCastException:java.math.BigInteger在连接MySQL时无法强制转换为java.lang.Long

ClassCastException:java.math.BigInteger在连接MySQL时无法强制转换为java.lang.Long

连接到MySQL时,出现错误(见下文).

单击here以获取代码

我得到这个输出:

run:
Now connecting to databse...

java.sql.sqlException: java.lang.classCastException: java.math.BigInteger cannot be cast to java.lang.Long
java.sql.sqlException: java.lang.classCastException: java.math.BigInteger cannot be cast to java.lang.Long
    at com.@R_301_5163@.jdbc.sqlError.createsqlException(sqlError.java:1074)
    at com.@R_301_5163@.jdbc.sqlError.createsqlException(sqlError.java:988)
    at com.@R_301_5163@.jdbc.sqlError.createsqlException(sqlError.java:974)
    at com.@R_301_5163@.jdbc.sqlError.createsqlException(sqlError.java:919)
    at com.@R_301_5163@.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1062)
    at com.@R_301_5163@.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3556)
    at com.@R_301_5163@.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2513)
    at com.@R_301_5163@.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
    at com.@R_301_5163@.jdbc.ConnectionImpl.301_5163@.jdbc.JDBC4Connection.java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.@R_301_5163@.jdbc.Util.handleNewInstance(Util.java:411)
    at com.@R_301_5163@.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
    at com.@R_301_5163@.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at dbms_basic.Dbms_Basic.main(Dbms_Basic.java:28)
Caused by: java.lang.classCastException: java.math.BigInteger cannot be cast to java.lang.Long
    at com.@R_301_5163@.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1007)
    ... 15 more
BUILD SUCCESSFUL (total time: 0 seconds)

我怎么解决这个问题?

最佳答案
您的错误清楚地表明无法进行转换,因为java.math.BigInteger类实例不是java.lang.Long类的实例.

现在出现的问题是谁在什么级别进行转换,当我们要求JDBC驱动程序建立连接时,它在场景之后做了很多工作,然后才真正给我们提供了正确的连接工作对象.

您的@R_301_5163@版本与您的mysql-connector.jar版本相结合似乎存在问题.尝试使用较新版本的@R_301_5163@ Connector / J(有关最新版本,请参阅https://dev.mysql.com/downloads/connector/j/),例如,如果您使用的是旧版本,请升级到5.1.47或8.0.12.

ClassCastException:不能在连接到MySQL时将java.math.BigInteger强制转换为java.lang.Long

ClassCastException:不能在连接到MySQL时将java.math.BigInteger强制转换为java.lang.Long

连接到MySQL时,出现错误(见下文)。

点击此处获取代码

我得到以下输出:

run:Now connecting to databse...java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Longjava.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)    at com.mysql.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1062)    at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3556)    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2513)    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)    at java.sql.DriverManager.getConnection(DriverManager.java:664)    at java.sql.DriverManager.getConnection(DriverManager.java:247)    at dbms_basic.Dbms_Basic.main(Dbms_Basic.java:28)Caused by: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long    at com.mysql.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1007)    ... 15 moreBUILD SUCCESSFUL (total time: 0 seconds)

我该如何解决?

答案1

小编典典

您的错误明确指出无法进行强制转换,因为java.math.BigInteger类实例不是java.lang.Long类的实例。

现在出现了一个问题,即谁在哪个级别上进行强制转换,当我们要求JDBC驱动程序进行连接时,它实际上在幕后进行了大量工作,然后才真正给我们适当的连接工作对象。

您的MySQL版本与mysql-connector.jar版本组合似乎出现了问题。尝试使用更高版本的MySQL Connector /
J(有关最新版本,请参见https://dev.mysql.com/downloads/connector/j/),例如,如果使用的是旧版本,请升级到5.1.47或8.0.12版。

ClassCastException:无法将java.lang.Object []强制转换为java.lang.String [] android

ClassCastException:无法将java.lang.Object []强制转换为java.lang.String [] android

在我的应用程序中,我需要将arraylist转换为数组的字符串。但是,我得到一个错误:

ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[] android

在与listofurls我在一起的错误:listofurls = (String[])image_urls.toArray();

这是完整的代码:

public class Test2 extends AsyncTask<Void, Void, Void> {  String[] listofurls ;  private static final String url = "http://www.tts.com/album_pro/array_to_encode";  JSONParser jParser = new JSONParser();  ArrayList<String> image_urls = new ArrayList<String>();  protected void onPreExecute() {    //Log.e(LOG_CLASS, "in side assyntask");  }  protected Void doInBackground(Void... voids) {    Log.v("Async","Async");    JSONObject json = jParser.getJSONFromUrl(url);    try {      JSONObject seo = json.getJSONObject("SEO");      JSONArray folio = seo.getJSONArray("Folio");      // JSONArray image_urls1 = new JSONArray();      //String s1=seo.getString("Folio");      for(int i=0;i<folio.length();++i) {        String m = folio.getString(i);        Log.v("M"+i,m);        image_urls.add(m);        Log("test-url"+image_urls);      }    } catch(Exception e) {      e.printStackTrace();    }    listofurls = (String[])image_urls.toArray();  //ERROR OCCURS HERE    return null;  }  private void Log(String string) {    Log.v("Test",string);  }  protected void onProgressUpdate(Integer... progress) { }  protected void onPostExecute(Void result) {    mAdapter = new ImagePagerAdapter(getSupportFragmentManager(),listofurls.length );    mAdapter.setImageurls(listofurls);    mPager.setAdapter(mAdapter);  }

答案1

小编典典

尝试

listofurls = image_urls.toArray(new String[image_urls.size()]);

注意:我建议将listofurls重命名为arrayOfURLs

关于ClassCastException:无法将java.util.Date强制转换为java.sql.Date无法将javac项识别为怎么改的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Android java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.EditText、ClassCastException:java.math.BigInteger在连接MySQL时无法强制转换为java.lang.Long、ClassCastException:不能在连接到MySQL时将java.math.BigInteger强制转换为java.lang.Long、ClassCastException:无法将java.lang.Object []强制转换为java.lang.String [] android的相关信息,请在本站寻找。

本文标签: