将Android应用程序连接到MYSQL数据库

时间:2016-05-10 05:41:42

标签: android mysql

当我尝试将android应用程序连接到MYSQL数据库服务器时,会发生以下错误。我正在linux工作室中使用android studio:

  

05-10 10:46:58.555 29599-29599 / com.example.pc.hms_treva D / Atlas:   验证地图... 05-10 10:46:58.660   29599-29619 / com.example.pc.hms_treva I / OpenGLRenderer:已初始化   EGL,版本1.4 05-10 10:46:58.743   29599-29619 / com.example.pc.hms_treva D / OpenGLRenderer:启用调试   模式0 05-10 10:46:58.787 29599-29619 / com.example.pc.hms_treva   W / EGL_emulation:eglSurfaceAttrib未实现05-10 10:46:58.787   29599-29619 / com.example.pc.hms_treva W / OpenGLRenderer:设置失败   表面上的EGL_SWAP_BEHAVIOR 0xaf0355c0,错误= EGL_SUCCESS

这是代码:

public class MainActivity extends Activity {
ImageView prev, now, next;
TextView display, bottom;

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

    prev = (ImageView) findViewById(R.id.previmg);
    now = (ImageView) findViewById(R.id.token);
    next = (ImageView) findViewById(R.id.nextimg);
    display = (TextView) findViewById(R.id.textView2);
    bottom = (TextView) findViewById(R.id.tokennum);

    prev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Connect runner = new Connect();
            //  String sleepTime = time.getText().toString();
            runner.execute();
        }
    });
    now.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i=new Intent(getApplicationContext(),StatusUpdate.class);
            startActivity(i);
        }
    });
}


class Connect extends AsyncTask<String, String, String> {

    MainActivity m = new MainActivity();

    @Override
    protected String doInBackground(String... params) {
        return null;

    }

    protected void onPreExecute() {
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();

        } catch (InstantiationException e1) {
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }
        String username = "qqq";
        String password = "dddd";
        Connection DbConn = null;
        try {
            DbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://xxx.xxx.x.x:1433/DATABASE NAME;user=" + username + ";password=" + password);
            Log.i("Connection","open");
            Toast.makeText(getApplicationContext(),"coonnected",Toast.LENGTH_LONG).show();
        } catch (SQLException e1) {

            e1.printStackTrace();
        }

        Log.w("Connection", "open");
        Statement stmt = null;
        try {
            stmt = DbConn.createStatement();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        ResultSet reset = null;
        try {
            reset = stmt.executeQuery(" select TOKENID from DEPTVISIT where DEPVISITID=1 ");
            display.setText(reset.getString(1));
        } catch (SQLException e1) {
            e1.printStackTrace();
        }


        try {
            DbConn.close();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }

    }
}

}

1 个答案:

答案 0 :(得分:0)

AsyncTask in android developer manual

你应该在doInBackground()而不是onPreExecute()中编写代码 因为onPreExecute()在UI线程上执行,对声明或创建进度对话框很有用

在doInBackground()中编写需要很长计算时间的代码,例如数据库查询或解析ResultSet或JSON等。

将android连接到mysql 从mysql下载mysql jar,然后将其作为jar模块导入项目中 或者您可以从gradle添加依赖项

然后您可以直接使用以下方法连接到数据库

        @Override
    protected String doInBackground(String... params) {
        String method=params[0];
            if(method.equals("register"))
            {
                String username=params[1];
                String password=params[2];

         try {       
               Class.forName("com.mysql.jdbc.Driver");
                connection=DriverManager.getConnection("jdbc:mysql://192.168.1.100:3306/dbname","username","password");

             Log.d("DB", "Connection succesfull");
             Log.d("DB","param1"+username);
             Log.d("DB","param2"+password);
             PreparedStatement pst = connection.prepareStatement("insert       into registertest values(?,?)");

             pst.setString(1,username);
             pst.setString(2,password);

             int x = pst.executeUpdate();
             connection.close();

             Log.d("DB",x + "record inserted");
             connection.close();
            } 
            catch (Exception e1) {
              String trace=e1.getStackTrace().toString();
              Log.d("DB", "SQL Exception"+trace,e1);
         }

     }

    return null;
}