无法在Android Studio中创建与数据库服务器的连接

时间:2018-06-29 15:32:23

标签: java android mysql

我一直试图连接到MySQL数据库,我编写了完整的数据库连接代码。

当前我现在遇到的错误是:

06-29 21:04:12.097 2410-2410/com.example.forhad.eye10 I/System.out: SELECT 
`UserEmail`,`UserName`, `Password`,`City` FROM `user`;
06-29 21:04:12.109 2410-2410/com.example.forhad.eye10 I/System.out: Driver 
Loaded
06-29 21:04:12.545 2410-2410/com.example.forhad.eye10 I/System.out: 
Exception: Could not create connection to database server.

我在项目中安装了mysql-connector并将其添加到类路径中(位于我的依赖项中)。这是我的Java代码:

package com.example.forhad.eye10;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class MainActivity extends Activity {
private EditText UserName,UserPassword;
private boolean flag;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    UserName = (EditText) findViewById(R.id.editText);
    UserPassword = (EditText) findViewById(R.id.editText2);

    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            check();
        }
    });
}

public void check(){
    String query = "SELECT `UserEmail`,`UserName`, `Password`,`City` FROM `user`;";
    Connection con = null;
    Statement st = null;
    ResultSet rs = null;
    System.out.println(query);

    try{
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Driver Loaded");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Letsgo2","root","");
        System.out.println("Connection Established");
        st = con.createStatement();
        System.out.println("Statement Created");
        rs = st.executeQuery(query);
        System.out.println("Results Received");

        while(rs.next()){
            String Name = rs.getString("UserName");
            String Password = rs.getString("Password");
            String City = rs.getString("City");
            String UserEmail = rs.getString("UserEmail");

            if (Name.equals(UserName.getText())){
                flag = false;
                if(Password.equals(UserPassword.getText())){
                    //UserHome ush = new UserHome(Name,Password,City);
                    //this.setVisible(false);
                    //ush.setVisible(true);
                }
                else{
                    Toast.makeText(MainActivity.this,"Invalid Password", Toast.LENGTH_SHORT).show();
                }
            }
        }
        if(flag){
            Toast.makeText(MainActivity.this,"Invalid UserName", Toast.LENGTH_SHORT).show();
        }
    } catch(Exception ex){
        System.out.println("Exception: "+ex.getMessage());
    }
    finally{
        try{
            if(rs!=null){
                rs.close();
                System.out.println("Results Set Closed");
            }
            if(st!=null){
                st.close();
                System.out.println("Statement Closed");
            }
            if(con!=null){
                con.close();
                System.out.println("Connection Closed");
            }
        }
        catch(Exception ex){}
    }
}

我的SQL Server似乎正在运行,说它是在线的。请帮我弄清楚我在做什么错。

2 个答案:

答案 0 :(得分:0)

由于您的JDBC URL看起来正确,因此您的服务器似乎没有运行。

仔细检查您的配置,即使错误消息与凭据没有直接关系,也请再次检查您配置的用户/密码。

我注意到您在select语句中使用了`。它有两个问题,首先是它不起作用,您应该使用'(单引号)代替。其次,如果您的目标是选择列,则实际上会添加未命名的列以及结果中的值。

示例:从表中选择“ id”,“用户”

?列? ?柱?    身份用户    身份用户

答案 1 :(得分:0)

出于安全原因和易于维护的原因,我认为与数据库进行Android连接的正确方法是使用WebService或API。试试吧。