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

Posted

技术标签:

【中文标题】在线程中执行 MySQL 存储过程并获取不支持的可调用语句【英文标题】:Executing MySQL stored procedure in thread and getting callable statements not supported 【发布时间】:2018-08-06 15:32:41 【问题描述】:

我搜索了如何解决这个问题,但没有找到解决方案。

我正在尝试使用 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;

谢谢

【问题讨论】:

仅供参考***.com/a/20867215/1207049 【参考方案1】:

来自这个链接:

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

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

【讨论】:

以上是关于在线程中执行 MySQL 存储过程并获取不支持的可调用语句的主要内容,如果未能解决你的问题,请参考以下文章

mysql中怎么存储数组

mysql存储过程。。

MySQL:MySQL 存储过程

mysql存储过程,选择最大值并插入值并分配给变量

php调用mysql存储过程

MySQL数据库高级——存储过程