在安卓模拟器中连接数据库(MySQL)来实现用户登录 求大神看哪里出错了
本帖最后由 iguyingfeng 于 2013-06-18 13:07:49 编辑 求大神帮忙看一下 从两个EditText中分别获取输入的用户名与密码 然后跟数据库(MySQL)中的用户名 密码比对
成功的话 跳转到另外一个Activity
public class LogOnActivity extends Activity {
private String username = "";
private String password = "";
private Intent intent = new Intent();
public void setViewTwoCommand() {
final Dialog alertDialog = new AlertDialog.Builder(this)
.setTitle(getString(R.string.alert)).setMessage(R.string.wrong)
.setIcon(R.drawable.ic_launcher).create();
Button buttonSure = (Button) findViewById(R.id.buttonSure);
Button buttonCancelButton = (Button) findViewById(R.id.buttonCancel);
final EditText editText1 = (EditText) findViewById(R.id.editText1);
final EditText editText2 = (EditText) findViewById(R.id.editText2);
buttonSure.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
username = editText1.getText().toString().trim();
password = editText2.getText().toString().trim();
Connection conn = null;
Statement stmt = null;
String sql = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "");
stmt = conn.createStatement();
sql = "Select * FROM user";
rs = stmt.executeQuery(sql);
while (rs.next()) {
if (username.equals(rs.getString("username")) && password.equals(rs.getString("password"))) {
//关键就是这里 没法从数据库比对 然后跳转另一个界面
intent.setClass(LogOnActivity.this,
test.logon.LogOn_CalculatorActivity.class);
LogOnActivity.this.startActivity(intent);
finish();
}
System.out.println(rs.getInt("id") + ","
+ rs.getString("username") + ","
+ rs.getString("password"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
buttonCancelButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
username = "";
password = "";
editText1.setText(username);
editText2.setText(password);
}
});
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logon);
setTheme(android.R.style.Theme_Black);
setViewTwoCommand();
}
}
[解决办法]
要连接mysql,我觉得应该通过一个服务器中转才行把。。
[解决办法]
你可以定义个webservice,调用然后判断返回的数据
[解决办法]
android 不能直接连外部数据库, 我试过, 直接连oracle会报错
LZ可以写一个中转服务器来实现, 比如用java socket jdbc 连数据库