Java MySQL和JFrame连接“无法从布尔值转换为连接”

时间:2015-06-19 04:48:46

标签: java android mysql swing import

公共课BancoDeDados {

static String url = "jdbc:mysql://localhost:3306/Estoque";
static String pass = "admin";
static String user = "admin";
static Connection conexao = null;

public static boolean conecta() throws ClassNotFoundException {

    try {
        Class.forName("mysql.Driver");
        conexao = DriverManager.getConnection(url, user, pass);
        //System.out.println("Conectado.");
        JOptionPane.showMessageDialog(null, "Conectado com Sucesso!");
        return true;
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "Usuário e/ou senha estão incorretos!");
        //System.out.println("Usuário e/ou senha estão errados.");
        return false;
    }

}

公共类TelaPrincipal扩展了JFrame {

Connection conexao = null;
PreparedStatement pst = null;
ResultSet rs = null;


private JPanel contentPane;
private JTextField textLogin;
private JPasswordField textSenha;





public void logar(){
    String sql = "Select *from Login WHERE Usuario = ? and senha = ?";
    try{
        pst = conexao.prepareStatement(sql);
        pst.setString(1, textLogin.getText());
        pst.setString(2, textSenha.getText());
        rs = pst.executeQuery();

        if(rs.next()){
            TelaOpcoes telaopcoes = new TelaOpcoes();
            telaopcoes.setVisible(true);
            dispose();      
        }
        else{
            JOptionPane.showMessageDialog(null, "Usuário e Senha Inválidos");
        }

    }

    catch(SQLException error){
        JOptionPane.showMessageDialog(null, error);

    }
}


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                TelaPrincipal frame = new TelaPrincipal();
                frame.setVisible(true);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public TelaPrincipal() throws ClassNotFoundException {

    conexao = BancoDeDados.conecta();
    this.setLocationRelativeTo(null);

它在倒数第二行上做错误“CONEXAO = BANCODEDADOS.CONECTA();”说“类型不匹配:无法从布尔值转换为连接” 有人可以帮帮我吗?谢谢!

4 个答案:

答案 0 :(得分:1)

CONEXAO = BANCODEDADOS.CONECTA();

这里CONEXAO是一个连接变量

但方法 BANCODEDADOS.CONECTA();返回一个布尔值。

所以改变变量CONEXAO的类型

或创建一个新的变量boolean类型来存储结果 Boolean conexao = BancoDeDados.conecta();

答案 1 :(得分:0)

conexaoConnection的对象,您要将方法调用的值BancoDeDados.conecta(); [返回boolean]分配给Connection

更改您的代码,如

Boolean result = BancoDeDados.conecta();

或者从方法conexao返回conecta以支持您的代码。

修改

连接MySQL的驱动程序是com.mysql.jdbc.Driver。更改您的代码

Class.forName("mysql.Driver");

Class.forName("com.mysql.jdbc.Driver");

答案 2 :(得分:0)

您的方法返回一个布尔值(不是Connection)。变化

public static boolean conecta() throws ClassNotFoundException {
    try {
        Class.forName("mysql.Driver");
        conexao = DriverManager.getConnection(url, user, pass);
        //System.out.println("Conectado.");
        JOptionPane.showMessageDialog(null, "Conectado com Sucesso!");
        return true;
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "Usuário e/ou senha estão incorretos!");
        //System.out.println("Usuário e/ou senha estão errados.");
        return false;
    }
}

类似

public static Connection conecta() {
    Connection conn = null;
    try {
        Class.forName("mysql.Driver");
        conn = DriverManager.getConnection(url, user, pass);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return conn;
}

答案 3 :(得分:0)

您正在从方法mail.php返回布尔值,并在构造函数中将其存储到conecta() ...

因此,将java.sql.Connection方法的返回类型更改为conecta() ..