2017-01-30 108 views
1

有人可以幫助我嗎?我ALWAYSE當我點擊登錄按鈕獲得這種例外喜歡的圖片波紋管:java.sql.jdbc.SQLException:連接到XAMPP時沒有合適的驅動程序MYSQL

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連接...

+1

推介讀數:http://stackoverflow.com/questions/15853367/jdbc-vs -web-service-for-android –

+0

給予所有堆棧跟蹤.... – ELITE

+0

您應該在libs文件夾中添加'mysql-connector.jar',但不建議使用mysql-connector ndroid ...閱讀以上評論@MorrisonChang – ELITE

回答

0

嗨Stevanus, 什麼莫里森和ELITE說的是對的。 我假設你正在連接到遠程mysql服務器(我的意思是,Mysql服務器沒有在你的android設備上運行)。 有兩種方式連接

  • 使用Web服務(SOAP或REST)
  • 下使用mysql-connector.jar

使用Web服務: 這是推薦的方式連接到任何數據庫服務器。 在這裏您將通過兩個步驟連接到數據庫。

  1. 使用Web服務,您會打的服務器端應用程序
  2. 應用將採取與數據庫服務器進行通信,並得到適當的響應,併發送回設備的響應的照顧。

使用Myql-connector.jar

這種做法OK,如果你只是從下面點的android device.Remember數據庫連接進行播放,而在這樣 1.首先連接您必須創建一個用戶在mysql服務器中,通過提及您的設備IP,您將從遠程計算機訪問Mysql Server,它需要遠程計算機IP,該計算機IP已在其用戶表中可用。 2.mysql-connector jar下classpath。

請參閱這些鏈接。

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, 阿賈伊

相關問題