扩展SqlDatabase类(扩展SQLiteOpenHelper)到另一个类文件(SqlServer连接)

时间:2019-07-09 16:45:17

标签: android-studio

我正在尝试开发一个Android应用。问题是我无法将sqldatabase用于Connection_Server类。

1)我尝试在Connection_Server类中设置“扩展AppCompatActivity”,然后在运行应用程序时收到错误消息(在LogCat中)。

  

无法在尚未调用Looper.prepare()的线程Thread [AsyncTask#1,5,main]中创建处理程序。

2)使用ALT + ENTER,我在UIImage中创建了创建者,但我不知道在Database_title.java中要写什么。

super()

这是我的Sql数据库类

public Database_title(Connection_Server connection_server) {
    super();
}

这里是我的sql类


import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.SQLException;
import android.icu.text.SimpleDateFormat;
import android.os.StrictMode;
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Date;
import java.util.Locale;

public class Connection_Server {
    private Database_title mDatabaseHelper;


    @SuppressLint("NewApi")
    public Connection CONN() {
        String ip = "46.246.205.218";
        String year_string = new SimpleDateFormat("yyyy", Locale.getDefault()).format(new Date());
        String classs = "net.sourceforge.jtds.jdbc.Driver";
        String db = "Main_"+year_string;
        String un = "David";
        String password = "*********";
        mDatabaseHelper = new Database_title(this);<-----HERE IS THE ERROR<------
        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection conn = null;
        String ConnURL;
        try {
            Class.forName(classs);

            Cursor res = mDatabaseHelper.getAllData_main();
            if (res.getCount()>0){
                switch (res.getString(2)){
                    case "text1":
                        db= "Database_Athens_Main"+year_string;
                        break;
                    case "text2":
                        db= "Database_Piraeus_Main"+year_string;
                        break;
                    case "text2":
                        db= "Database_South_Main"+year_string;
                        break;
                    case "text3":
                        db= "Database_North_Main"+year_string;
                        break;
                    case "text4":
                        db= "Database_West_Main"+year_string;
                        break;
                }
                Log.i("String Db","String DB: "+db);
            }else{
                db= "Database_Piraeus";
                Log.i("String Db","String DB: DEFAULT SET!!");
            }

            ConnURL = "jdbc:jtds:sqlserver://" + ip + ";" + "databaseName=" + db + ";user=" + un + ";password=" + password + ";";
            conn = DriverManager.getConnection(ConnURL);
        }
        catch (SQLException se)
        {
            Log.e("Error 1: ", se.getMessage());
        }
        catch (ClassNotFoundException e)
        {
            Log.e("Error 2: ", e.getMessage());
        }
        catch (Exception e)
        {
            Log.e("Error 3: ", e.getMessage());
        }
        return conn;
    }
}

如果我不做1或2,则在构建时会出现以下错误:

  

任务:app:compileDebugJavaWithJavac FAILED失败   C:\ Users \ hwa_r \ AndroidStudioProjects \ Dias_APP \ app \ src \ main \ java \ com \ example \ dias_app \ Connection_Server.java:27:错误:不兼容的类型:Connection_Server无法转换为上下文           mDatabaseHelper = new Database_title(this);

0 个答案:

没有答案