【发布时间】:2015-07-01 20:42:17
【问题描述】:
我正在尝试将我的应用程序连接到我的电脑中的 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;
}
}
提前致谢:)
【问题讨论】: