我正在尝试直接从我的应用程序访问一个在线数据库到没有网络服务的数据库...我意识到这是多么愚蠢,但它只是一个项目演示。任何帮助/建议表示赞赏:)
以下是我访问数据库的代码:
namespace OmnicalMobile
{
[Activity(Label = "Omnical",MainLauncher =true, Icon = "@drawable/icon" )]
public class MainActivity : Activity
{
public EditText txtNumber;
private Button btnYo;
private EditText txtPassword;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
//ActionBar.Dispose();
btnYo = FindViewById<Button>(Resource.Id.logBtn);
txtNumber = FindViewById<EditText>(Resource.Id.editText3);
txtPassword = FindViewById<EditText>(Resource.Id.editText4);
btnYo.Click += BtnYo_Click;
// Get our button from the layout resource,
// and attach an event to it
}
private void BtnYo_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection("(HIDDEN FROM INTERNET GRIEFERS)");
MySqlCommand command;
try
{
if (con.State == ConnectionState.Closed)
{
Toast.MakeText(Application.Context, "Connection success", ToastLength.Short).Show();
MySqlDataReader reader = null;
String storedPassword ="";
MySqlCommand cmd = new MySqlCommand("SELECT COUNT(*) FROM student WHERE student_number ='" + txtNumber.Text + "'", con);
con.Open();
int count = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
if (count != 0)
{
String SQLQuery = "SELECT password FROM student WHERE student_number ='" + txtNumber.Text + "' AND course = 'Information Technology'";
command = new MySqlCommand(SQLQuery, con);
con.Open();
command.ExecuteNonQuery();
reader = command.ExecuteReader();
while (reader.Read())
{
storedPassword = (string)reader["password"];
}
testPassword(txtNumber.Text, txtPassword.Text, storedPassword);
}
else
{
Toast.MakeText(Application.Context, "No such student exists", ToastLength.Short).Show();
txtPassword.Text = null;
txtNumber.Text = null;
}
}
}
catch (MySqlException ex)
{
Toast.MakeText(Application.Context, ex.Message, ToastLength.Short).Show();
}
finally
{
con.Close();
}
}
private void testPassword(String userNumber, String password, String storedPassword)
{
if (password.Equals(storedPassword))
{
string number = txtNumber.Text;
var second = new Intent(this, typeof(Activity1));
second.PutExtra("Number", number);
Finish();
StartActivity(second);
}
else
{
Toast.MakeText(Application.Context, "Incorrect Password", ToastLength.Short).Show();
txtPassword.Text = null;
txtNumber.Text = null;
}
}
}
以下是崩溃的logcat报告:
10-02 23:16:27.742 I/MonoDroid(16374): UNHANDLED EXCEPTION:
10-02 23:16:27.752 I/MonoDroid(16374): System.NotSupportedException: No data is available for encoding 1252.
10-02 23:16:27.752 I/MonoDroid(16374): at System.Text.Encoding.GetEncoding (Int32 codepage) <0xeeb7e920 + 0x00648> in <filename unknown>:0
10-02 23:16:27.752 I/MonoDroid(16374): at System.Text.Encoding.GetEncoding (System.String name) <0xeeb7ced8 + 0x00033> in <filename unknown>:0
10-02 23:16:27.752 I/MonoDroid(16374): at MySql.Data.MySqlClient.Driver..ctor (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) <0xeeb7ccf8 + 0x00023> in <filename unknown>:0
10-02 23:16:27.752 I/MonoDroid(16374): at MySql.Data.MySqlClient.Driver.Create (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) <0xeeb7ca18 + 0x00073> in <filename unknown>:0
10-02 23:16:27.752 I/MonoDroid(16374): at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection () <0xeeb7c9c8 + 0x00013> in <filename unknown>:0
10-02 23:16:27.752 I/MonoDroid(16374): at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection () <0xeeb7c408 + 0x0022b> in <filename unknown>:0
10-02 23:16:27.752 I/MonoDroid(16374): at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver () <0xeeb7c1e0 + 0x00057> in <filename unknown>:0
10-02 23:16:27.752 W/art (16374): JNI RegisterNativeMethods: attempt to register 0 native methods for android.runtime.JavaProxyThrowable
10-02 23:16:27.752 D/AndroidRuntime(16374): Shutting down VM
10-02 23:16:27.762 E/AndroidRuntime(16374): FATAL EXCEPTION: main
10-02 23:16:27.762 E/AndroidRuntime(16374): Process: OmnicalMobile.OmnicalMobile, PID: 16374
10-02 23:16:27.762 E/AndroidRuntime(16374): java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
10-02 23:16:27.762 E/AndroidRuntime(16374): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
10-02 23:16:27.762 E/AndroidRuntime(16374): Caused by: java.lang.reflect.InvocationTargetException
10-02 23:16:27.762 E/AndroidRuntime(16374): at java.lang.reflect.Method.invoke(Native Method)
10-02 23:16:27.762 E/AndroidRuntime(16374): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
10-02 23:16:27.762 E/AndroidRuntime(16374): ... 1 more
10-02 23:16:27.762 E/AndroidRuntime(16374): Caused by: android.runtime.JavaProxyThrowable: System.NotSupportedException: No data is available for encoding 1252.
10-02 23:16:27.762 E/AndroidRuntime(16374): at System.Text.Encoding.GetEncoding (Int32 codepage) <0xeeb7e920 + 0x00648> in <filename unknown>:0
10-02 23:16:27.762 E/AndroidRuntime(16374): at System.Text.Encoding.GetEncoding (System.String name) <0xeeb7ced8 + 0x00033> in <filename unknown>:0
10-02 23:16:27.762 E/AndroidRuntime(16374): at MySql.Data.MySqlClient.Driver..ctor (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) <0xeeb7ccf8 + 0x00023> in <filename unknown>:0
10-02 23:16:27.762 E/AndroidRuntime(16374): at MySql.Data.MySqlClient.Driver.Create (MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) <0xeeb7ca18 + 0x00073> in <filename unknown>:0
10-02 23:16:27.762 E/AndroidRuntime(16374): at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection () <0xeeb7c9c8 + 0x00013> in <filename unknown>:0
10-02 23:16:27.762 E/AndroidRuntime(16374): at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection () <0xeeb7c408 + 0x0022b> in <filename unknown>:0
10-02 23:16:27.762 E/AndroidRuntime(16374): at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver () <0xeeb7c1e0 + 0x00057> in <filename unknown>:0
10-02 23:16:27.762 E/AndroidRuntime(16374): at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
10-02 23:16:27.762 E/AndroidRuntime(16374): at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:30)
10-02 23:16:27.762 E/AndroidRuntime(16374): at android.view.View.performClick(View.java:5697)
10-02 23:16:27.762 E/AndroidRuntime(16374): at android.widget.TextView.performClick(TextView.java:10813)
10-02 23:16:27.762 E/AndroidRuntime(16374): at android.view.View$PerformClick.run(View.java:22526)
10-02 23:16:27.762 E/AndroidRuntime(16374): at android.os.Handler.handleCallback(Handler.java:739)
10-02 23:16:27.762 E/AndroidRuntime(16374): at android.os.Handler.dispatchMessage(Handler.java:95)
10-02 23:16:27.762 E/AndroidRuntime(16374): at android.os.Looper.loop(Looper.java:158)
10-02 23:16:27.762 E/AndroidRuntime(16374): at android.app.ActivityThread.main(ActivityThread.java:7224)
10-02 23:16:27.762 E/AndroidRuntime(16374): ... 3 more
10-02 23:16:27.762 W/ActivityManager( 3452): Force finishing activity OmnicalMobile.OmnicalMobile/md5d0cf5a481b8ca811cb223058ac8e5810.MainActivity
答案 0 :(得分:0)
使用线程运行此命令,您将获得解决方案。(Java和Java android)
new Thread(new Runnable(){
public void run() {
your_function();
}
});