gpt4 book ai didi

java - gradle显示兼容性错误

转载 作者:行者123 更新时间:2023-12-02 12:06:41 25 4
gpt4 key购买 nike

我创建了 Android 应用程序。我想显示来 self 的 aws mysql 数据库的数据。我写了以下代码:

package com.example.yash.car2;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import java.sql.*;


public class MainActivity extends AppCompatActivity{

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

init();
}



public Connection CreateDBconn()
{
try
{
String DRIVER_NAME = "com.mysql.jdbc.Driver";
String HOST_NAME = "firstdb.cqmpw4u1tyz3.us-west-2.rds.amazonaws.com";
String PORT_NUMBER = "3306";
String DB_NAME = "test_master";
String USER_NAME = "*****";
String PASSWORD = "*****";
String URL="jdbc:mysql://firstdb.cqmpw4u1tyz3.us-west-2.rds.amazonaws.com/test_master";
Class.forName(DRIVER_NAME);
Connection conn = DriverManager.getConnection(URL,USER_NAME,PASSWORD);
return conn;

}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}

public void init() {
try
{
Connection c=CreateDBconn();
String query="SELECT * FROM STUD";
Statement st=c.createStatement();
ResultSet rs=st.executeQuery(query);

TableLayout stk = (TableLayout) findViewById(R.id.TL);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText(" Sl.No ");
tv0.setTextColor(Color.WHITE);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" Last Name ");
tv1.setTextColor(Color.WHITE);
tbrow0.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(" First Name ");
tv2.setTextColor(Color.WHITE);
tbrow0.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(" Age ");
tv3.setTextColor(Color.WHITE);
tbrow0.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setText(" Branch ");
tv4.setTextColor(Color.WHITE);
tbrow0.addView(tv4);
stk.addView(tbrow0);

while (rs.next()) {
TableRow tbrow = new TableRow(this);
TextView t1v = new TextView(this);
t1v.setText(rs.getInt("id"));
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);
TextView t2v = new TextView(this);
t2v.setText(rs.getString("LastName"));
t2v.setTextColor(Color.WHITE);
t2v.setGravity(Gravity.CENTER);
tbrow.addView(t2v);
TextView t3v = new TextView(this);
t3v.setText(rs.getString("FirstName"));
t3v.setTextColor(Color.WHITE);
t3v.setGravity(Gravity.CENTER);
tbrow.addView(t3v);
TextView t4v = new TextView(this);
t4v.setText(rs.getInt("Age"));
t4v.setTextColor(Color.WHITE);
t4v.setGravity(Gravity.CENTER);
tbrow.addView(t4v);
TextView t5v=new TextView(this);
t5v.setText(rs.getString("Branch"));
t5v.setTextColor(Color.WHITE);
t5v.setGravity(Gravity.CENTER);
tbrow.addView(t5v);
stk.addView(tbrow);
}}
catch (Exception e)
{
e.printStackTrace();
}

}}

我的代码显示以下错误:

1:Error:Error converting bytecode to dex: Cause: Dex cannot parse version 52 byte code. This is caused by library dependencies that have been compiled using Java 8 or above. If you are using the 'java' gradle plugin in a library submodule add targetCompatibility = '1.7' sourceCompatibility = '1.7' to that submodule's build.gradle file.

2.Error:Execution failed for task ':app:transformClassesWithDexForDebug'.

com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: Return code 1 for dex process

我对所有数据库的事情都很陌生。我猜问题出在 java.sql 的导入中,但我没有得到任何解决方案。

请帮忙!

最佳答案

您需要使用 Java 8,因为您的代码依赖于 Java 8 依赖项。

android {
compileSdkVersion 26
buildToolsVersion "26.0.2"

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

也使用最新的 gradle 插件。

classpath 'com.android.tools.build:gradle:3.3.3'

关于java - gradle显示兼容性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46864409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com