如何在Java中进行MySQL JDBC连接?

问题描述 投票:0回答:1

我的java连接器类似乎有问题:

import java.sql.DriverManager;
import javax.swing.JOptionPane;

public class Connection_DB {
    public static com.mysql.jdbc.Connection conn;
    public static void openConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/java_db";
            conn = (com.mysql.jdbc.Connection) DriverManager.getConnection(url, "root", "");

        } catch (ClassNotFoundException e) {} catch (java.sql.SQLException e) {
            JOptionPane.showMessageDialog(null, "Can't Connect");
        }
    }
    public static com.mysql.jdbc.Connection getConnection() {
        return conn;
    }
}

不知怎的,我无法连接它:

try {
    Koneksi_DB.openConnection();
    Statement statement = (Statement) Koneksi_DB.getConnection().createStatement();
    ResultSet result = statement.executeQuery("SELECT * FROM login_tb WHERE " + " username = '" + textFieldUsername.getText() + "'");
    if (result.next()) {
        if (textFieldPassword.getText().equals(result.getString("password"))) {
            System.out.println("Sucessfully logged in!");
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(rootPane, "Wrong password!");
            textFieldPassword.setText("");
            textFieldPassword.requestFocus();
        }
    } else {
        JOptionPane.showMessageDialog(rootPane, "User not found!");
        textFieldUsername.setText("");
        textFieldPassword.setText("");
        textFieldUsername.requestFocus();
    }
} catch (Exception e) {
    JOptionPane.showMessageDialog(null, e);
    JOptionPane.showMessageDialog(rootPane, "Connection failed!");
}

它给了我这个错误:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class com.mysql.cj.jdbc.ConnectionImpl cannot be cast to class com.mysql.jdbc.Connection (com.mysql.cj.jdbc.ConnectionImpl and com.mysql.jdbc.Connection are in unnamed module of loader 'app')
    at Koneksi_DB.openConnection(Koneksi_DB.java:10)
    at Form_Login.<init>(Form_Login.java:21)
    at Form_Login$4.run(Form_Login.java:169)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

我该如何解决这个问题?是否仍然可以使用原始的

Connection_DB
类代码使用
com.mysql.jdbc.Connection

java mysql database jdbc
1个回答
1
投票

请勿将

com.mysql.jdbc.Connection
用于您的字段类型。相反,请使用
java.sql.Connection

我建议你查找

ClassCastException
学习一些东西

© www.soinside.com 2019 - 2024. All rights reserved.