Немного запутался в ООП, подскажите пожалуйста
в MainActivity след код
Код: Выделить всё
// Инициализируем наш класс-обёртку
DBase sqh = new DBase(this);
// База нам нужна для записи и чтения
SQLiteDatabase sqdb = sqh.getWritableDatabase();
DBResult=sqh.connect(sqdb);
в DBase делаю метод connect
Код: Выделить всё
public int connect(SQLiteDatabase sqdb) {
try {
Thread t = new Thread(new Runnable() {
public void run() {
Log.d(LOG_TAG,">>> Thread is created");
Connection conn = null;
String sServerName = "10.0.2.2:3306";
String sDBName = "jugendkultur";
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "");
properties.setProperty("useUnicode", "true");
properties.setProperty("characterEncoding", "UTF-8");
properties.setProperty("autoReconnect", "true");
try {
Class.forName("com.mysql.jdbc.Driver");
Log.d(LOG_TAG,">>> jdbc.Driver: instance successful");
} catch (Exception e) {
Log.d(LOG_TAG,">>> jdbc.Driver: instance error");
e.printStackTrace();
}
try {
conn=DriverManager.getConnection("jdbc:mysql://"+sServerName+"/"+sDBName,properties);
//if(conn!=null) {
Log.d(LOG_TAG,">>> jdbc.Driver: connect successful");
//}
String query = "SELECT id, title FROM `tl_page` WHERE title='Jugend und Kultur ";
Language=DE;
switch(Language){
case DE:
query+="deu'";
break;
case EN:
query+="eng'";
break;
default:
Log.d(LOG_TAG,">>> Error: unbekannte Sprache");
}
Log.d(LOG_TAG,">>> Query: "+query);
Statement s;
try {
s=conn.createStatement();
s.executeQuery(query);
ResultSet rs;
rs = s.getResultSet();
// ~~~~~~~~~ Запись в локальную базу данных ~~~~~~~~~~~~~~~~
String TABLE_NAME = "tl_page";
String ID = "id";
String PID = "pid";
String TITLE = "title";
String SQL_CREATE_ENTRIES="CREATE TABLE IF NOT EXISTS "
+ TABLE_NAME+" ("+ID+" INT, "
+ PID+" INT, "
+ TITLE+" VARCHAR(255))";
sqdb.execSQL(SQL_CREATE_ENTRIES);
Log.d(LOG_TAG,"### Created: "+SQL_CREATE_ENTRIES+" ###");
// sqdb.close();
// sqh.close(); */
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
while (rs.next()) {
int mid=rs.getInt("id");
String title = rs.getString("title");
Log.d(LOG_TAG,"["+title+"] (id = "+mid+")");
}
} catch (SQLException e) {
Log.d(LOG_TAG,">>> createStatement: error");
System.out.println(">SQLException: "+e.getMessage());
System.out.println(">SQLState: "+e.getSQLState());
System.out.println(">VendorError: "+e.getErrorCode());
e.printStackTrace(); }
}
catch (SQLException e) {
Log.d(LOG_TAG,">>> jdbc.Driver: connect error");
System.out.println(">SQLException: "+e.getMessage());
System.out.println(">SQLState: "+e.getSQLState());
System.out.println(">VendorError: "+e.getErrorCode());
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
}
catch (SQLException e) {
Log.d(LOG_TAG,">>> jdbc.Driver: close error");
System.out.println(">SQLException: "+e.getMessage());
System.out.println(">SQLState: "+e.getSQLState());
System.out.println(">VendorError: "+e.getErrorCode());
e.printStackTrace();
}
catch (NullPointerException e) {
Log.d(LOG_TAG,">>> jdbc.Driver: close error (попытка закрытия не созданного соединения)");
System.out.println(">NullPointerException: "+e.getMessage());
e.printStackTrace();
}
}
});
t.start();
return DB_RESULT_OK;
}
catch (Exception e) {
e.printStackTrace();
Log.d(LOG_TAG,">>> Thread: create error");
return DB_RESULT_ERROR;
}
}
тут в потоке переписываю данные из внешней бд в локальную
пока всё грубо на код не обращайте внимания. Проблема в другом
компилятор говорит, что тут надо final
public int connect(
final SQLiteDatabase sqdb)
но, тогда бд получается константой и я не могу её править
Как править именно ту бд, которую я сюда передаю?
да и при вызове
DBResult=sqh.connect(sqdb);
можно как-то в java передавать не всю базу а только ссылку на неё?