在线程中执行MySQL存储过程并获取不支持的可调用语句

时间:2018-08-06 15:32:42

标签: java android mysql jdbc

我搜索了如何解决此问题,但没有找到解决方法。

我正在尝试使用Android Java在mysql中执行存储的过程。我只用Java尝试了一下,效果很好,但是在Android上却遇到了这个错误:

08-06 15:21:56.485 4057-4092/com.example.ik.myapplication E/+====/>: S1C00
08-06 15:21:56.485 4057-4092/com.example.ik.myapplication E/+====/>: Callable statments not supported.
08-06 15:21:56.485 4057-4092/com.example.ik.myapplication W/System.err:java.sql.SQLException: Callable statments not supported.
08-06 15:21:56.485 4057-4092/com.example.ik.myapplication W/System.err:at com.mysql.jdbc.Connection.prepareCall(Connection.java:1284)
08-06 15:21:56.485 4057-4092/com.example.ik.myapplication W/System.err:at com.example.ik.myapplication.MainActivity$1$1.run(MainActivity.java:61)
08-06 15:21:56.485 4057-4092/com.example.ik.myapplication W/System.err:at java.lang.Thread.run(Thread.java:761)

这是我的代码
MainActivity.java:

package com.example.ik.myapplication;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.util.ArrayList;
   public class MainActivity extends AppCompatActivity {
    AdminPage test = new AdminPage();
    public String md5(String text) throws NoSuchAlgorithmException {
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    messageDigest.reset();
    messageDigest.update(text.getBytes());
    byte[] digest = messageDigest.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    String hashtext = bigInt.toString(16);
    while (hashtext.length() < 32) {
        hashtext = "0" + hashtext;
    }
    return (hashtext);
}

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



    final EditText input_username = (EditText) findViewById(R.id.input_username);
    final EditText input_password = (EditText) findViewById(R.id.input_password);
    final Button login = (Button) findViewById(R.id.login_button);
    login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (input_username.getText().toString().equals("admin")) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Class.forName("com.mysql.jdbc.Driver");
                            final dbconnect dbconnect = null;
                            final Statement statement = dbconnect.dbconnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                            Log.e("+===>","before CallableStatement");
                            String query = "call loginadmin(?,?,?)";
                            CallableStatement cs=dbconnect.dbconnection().prepareCall(query);
                            cs.setString(1,input_username.getText().toString());
                            cs.setString(2,input_password.getText().toString());
                            cs.registerOutParameter(3,Types.INTEGER);
                            Log.e("+===>","after CallableStatement");
                            cs.execute();
                             final int logging =cs.getInt(3);
                            Log.e("+====/>", String.valueOf(logging));
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {

                                        if(logging==1){
                                            Intent intent = new Intent(getApplicationContext(), AdminPage.class);
                                            Toast.makeText(MainActivity.this, "WELCOME TO ADMIN PAGE", Toast.LENGTH_LONG).show();
                                            intent.putExtra("admin_username",input_username.getText().toString());
                                            startActivity(intent);
                                            finish();
                                        } else {
                                            Toast.makeText(MainActivity.this, "Check Your Informations , Please", Toast.LENGTH_LONG).show();

                                        }

                                }
                            });
                        } catch (SQLException e) {
                            Log.e("+====/>",e.getSQLState());
                            Log.e("+====/>",e.getMessage());
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();


            } else {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Class.forName("com.mysql.jdbc.Driver");
                            final dbconnect dbconnect = null;
                            final Statement statement = dbconnect.dbconnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                            String sqlquery = "SELECT * FROM `accounts`";
                            final ResultSet resultSet = statement.executeQuery(sqlquery);
                            Account accounts;
                            String uuid;
                            final ArrayList<Account> users = new ArrayList<Account>();
                            while (resultSet.next()) {

                                accounts = new Account(resultSet.getString(1), resultSet.getString(3), resultSet.getString(4), resultSet.getString(5), resultSet.getString(6), resultSet.getString(7));
                                users.add(accounts);
                            }
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    int i = 0;
                                    while (i < users.size()) {
                                        try {
                                            if (users.get(i).getUsername().equals(input_username.getText().toString()) && users.get(i).getPassword().equals(md5(input_password.getText().toString()))) {
                                                Intent intent = new Intent(getApplicationContext(), UsersPage.class);
                                                Toast.makeText(MainActivity.this, "Welcome to your page " + input_username.getText().toString(), Toast.LENGTH_LONG).show();
                                                try {
                                                    dbconnect.dbconnection().close();
                                                    statement.close();
                                                    resultSet.close();
                                                } catch (SQLException e) {
                                                    e.printStackTrace();
                                                }
                                                intent.putExtra("uuid", users.get(i).getUuid());
                                                intent.putExtra("username", users.get(i).getUsername());
                                                intent.putExtra("salary", users.get(i).getSalary());
                                                intent.putExtra("statue", users.get(i).getStatue());
                                                startActivity(intent);
                                                finish();
                                                break;
                                            }
                                        } catch (NoSuchAlgorithmException e) {
                                            e.printStackTrace();
                                        }
                                        i++;
                                    }
                                    if (i == users.size()) {
                                        Toast.makeText(MainActivity.this, "Check your informations please", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        } catch (SQLException | ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();


            }
        }
    });
    Button forgotten = (Button) findViewById(R.id.forgot);
    forgotten.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent forgotten = new Intent(getApplicationContext(), ForgottenPassword.class);
            startActivity(forgotten);
            finish();

        }
    });

}

}

这是我的dbconnect类:

package com.example.ik.myapplication;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class dbconnect {
private static String username;
private static String password;
private static String mysql;
public static Connection dbconnection() throws SQLException {
    mysql="jdbc:mysql://10.0.2.2:3306/enterprise";
    username="root";
    password="borni2019";
    return DriverManager.getConnection(mysql,username,password);
}

}

这是我的存储过程:

create procedure loginadmin(IN username varchar(50), IN password varchar(50), OUT confirmed int)
begin

      if exists(select * from admin_account where enterprise.admin_account.Username = username AND enterprise.admin_account.Password=password) then
         set confirmed=1;
        else
           set confirmed=0;
      end if;

end;

谢谢

1 个答案:

答案 0 :(得分:0)

通过此链接:

https://coderanch.com/t/305887/databases/SQLException-Callable-statments-supported

似乎您在Android上使用的MySQL驱动程序版本较旧。你可以尝试一个新的吗?