我试图将我的应用程序连接到我的电脑中的Xampp mysql服务器。问题是,如果不知道我是否需要使用SQLiteOpenHelper是或是?或者我可以在主要活动中做所有事情?这是我的代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (conectarMySql()){
Toast.makeText(getApplicationContext(), "WORKS", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "DONT WORK", Toast.LENGTH_SHORT).show();
}
}
});
}
public boolean conectarMySql() {
String xServidor = "10.0.2.2";
String xPuerto = "3306";
String xUsuario = "user1";
String xPass = "pass1";
String notwork = "not work";
String xBase = "penia";
String estado = "";
boolean estadoConexion = false;
Connection conexionMySql = null;
String driver = "com.mysql.jdbc.Driver";
String urlMySQL;
urlMySQL = "jdbc:mysql://" + xServidor + ":" + xPuerto + "/";
try {
Class.forName(driver).newInstance();
conexionMySql = DriverManager.getConnection(urlMySQL + xBase, xUsuario, xPass);
if (!conexionMySql.isClosed()){
Log.e(notwork,"CONEXION BIEN");
estadoConexion = true;
estado = "bien conexion";
}
}catch (Exception EX){
Log.e(notwork,"Con");
}
Log.e(notwork,estado);
return estadoConexion;
}
}
提前致谢:)
答案 0 :(得分:0)
在应用程序中创建SQLite数据库时,仅使用SQLiteOpenHelper 。如果您想连接到应用程序之外的数据库,那么您就不需要它了。