This question already has answers here:
Connect Java to a MySQL database
                                
                                    (13个回答)
                                
                        
                                2年前关闭。
            
                    
有人能帮我吗 ?当我单击“登录”按钮时,我总是收到像图片波纹管这样的异常:

enter image description here

此代码用于MainActivity:

package com.TPK.SistemPendataanPelalatan;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class MainActivity extends Activity {

    TextView ket;
    EditText id_user,pass;
    Button btnlogin;



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

        //deklarasi input dan output

        ket =(TextView) findViewById(R.id.warning_login);
        id_user = (EditText) findViewById(R.id.singup_id);
        pass = (EditText) findViewById(R.id.input_pass);
        btnlogin = (Button) findViewById(R.id.login);


    // event tombol ditekan
    btnlogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String id=id_user.getText().toString();
            String pw=pass.getText().toString();

            int salah=0;//hitung jumlah kesalahan
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            try {

                Statement st=(Statement)hubung.GetConnection().createStatement();

                if(id_user.getText().toString().isEmpty()||pass.getText().toString().isEmpty()){
                    ket.setText("ID dan Password Harus di Isi!");
                }else{
                ResultSet rs=st.executeQuery("select * from user where id='"+id+"'and password='"+pw+"'");
                if(rs.next()){
                    //kalo sukses masuk
                    ket.setText("Selamat Datang ...");
                    String nama=rs.getString(1);
                    if(rs.getString(5)=="1"){//ADMIN
                        Intent i = new Intent(MainActivity.this, activity_user_admin.class);
                        i.putExtra("nama", nama);//passing nama ke activity user admin
                        startActivity(i);
                        st.close();
                    }else if(rs.getString(5)=="2"){//SUPERVISOR
                        Intent i = new Intent(MainActivity.this, activity_user_super.class);
                        i.putExtra("nama", nama);//passing nama ke activity user super
                        startActivity(i);
                        st.close();
                    }else if(rs.getString(5)=="3"){//TEKNISI
                        Intent i = new Intent(MainActivity.this, activity_user_teknisi.class);
                        i.putExtra("nama", nama);//passing nama ke activity user teknisi
                        startActivity(i);
                        st.close();
                    }
                }
                else {
                    ket.setText("ID atau Password Salah !");
                    salah++;
                    if(salah>3){
                        salah=0;
                        ket.setText("Silahkan Lapor Admin");
                    }

                }
                }

            } catch (SQLException e) {
                // TODO Auto-generated catch block
                ket.setText(e.toString());
            }
        }
    });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}


这是我叫jdbc以名称hubung连接到mysql的地方:

package com.TPK.SistemPendataanPelalatan;
import java.sql.*;

public class hubung {
    private static Connection kon;
    public static Connection GetConnection() throws SQLException{
      if(kon==null){
        kon = DriverManager.getConnection("jdbc:mysql://10.11.1.232:3306/tpk_peralatan","root","");
      }
      return kon;

    }
}


谢谢 ...

注意:此程序可以构建并运行良好,但卡在mysql连接中...

最佳答案

您好Stevanus,
             莫里森和精英所说的都是对的。
我假设您正在连接到远程mysql服务器(我的意思是,Mysql服务器未在您的android设备中运行)。
有两种连接方式


使用Web服务(SOAP或REST)
使用mysql-connector.jar


使用Web服务:
                    建议使用此方法连接到任何数据库服务器。
在这里,您将分两步连接到数据库。


使用Web服务,您将访问服务器端应用程序
应用程序将与数据库服务器进行通信,并获取适当的响应并将响应发送回设备。


使用Myql-connector.jar

如果您只是在玩来自android设备的数据库连接,这种方法还可以。以这种方式连接时请记住以下几点
1.首先您必须在mysql服务器中创建一个用户,通过提及设备IP来进行访问,因为您将要从远程计算机访问Mysql Server,它需要远程计算机IP,该IP已在其用户表中提供。
2. mysql-connector jar在类路径下。

请参考这些链接。

1. http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/

2. https://www.quora.com/How-can-I-connect-send-and-retrieve-data-between-an-Android-device-and-a-remote-PC-with-a-PHP-interface-when-I-use-MySQL-as-my-database

3. https://www.youtube.com/watch?v=VM9wW3W22IE

Regads,
阿杰

10-07 13:08
查看更多