尝试使用MySQL连接时Android应用程序停止

时间:2016-02-08 17:33:01

标签: android mysql

我正在尝试开发一个应用程序作为练习,我想连接到MySQL数据库并执行一个插入语句,看起来很简单......但我真的很困惑,因为当我按下按钮执行插入时应用程序停止。

有人能帮助我吗?!?

提前致谢。

我的代码:

package com.mysql.javi.connectotomysqldb;


import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.os.AsyncTask;
import android.widget.Toast;
import java.sql.*;

public class MainActivity extends AppCompatActivity {


    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */

    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setContentView(R.layout.activity_main);

        btn=(Button)findViewById(R.id.registerButton);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "YOUR MESSAGE", Toast.LENGTH_LONG).show();

              ConnectionThread ct =  new ConnectionThread();
                ct.execute();

            }
        });

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    class ConnectionThread extends AsyncTask {

        @Override
        public Object doInBackground(Object[] params) {

            final String url = "jdbc:mysql://{server}/{dbName}";
            final String user = "user";
            final String password = "password";



            Connection con = null;
            try {

                Class.forName("com.mysql.jdbc.Driver").newInstance();

                con = DriverManager.getConnection(url, user, password);
                Statement st = con.createStatement();

                st.execute("INSERT INTO `Users`(`UserName`, `Pass`, `Mail`) " +
                        "VALUES ('AnUserName','admin1234','admin@gmail.com)");

                return 1;

            } catch (Exception e) {
                e.printStackTrace();
                return -1;
            } finally {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }



    }

}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.mysql.javi.connectotomysqldb.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

ADDED(记录消息):

Target device: myDevice
No apk changes detected since last installation, skipping installation of /Users/Javi/AndroidStudioProjects/ConnectoToMySQLDB/app/build/outputs/apk/app-debug.apk
Force stopping package: com.mysql.javi.connectotomysqldb
DEVICE SHELL COMMAND: am force-stop com.mysql.javi.connectotomysqldb
Launching application: com.mysql.javi.connectotomysqldb/com.mysql.javi.connectotomysqldb.MainActivity.
DEVICE SHELL COMMAND: am start  -n "com.mysql.javi.connectotomysqldb/com.mysql.javi.connectotomysqldb.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.mysql.javi.connectotomysqldb/.MainActivity }

0 个答案:

没有答案