【问题标题】:JDBC Driver error for mysql connectionmysql连接的JDBC驱动程序错误
【发布时间】:2013-03-26 16:23:32
【问题描述】:

我想用简单的 jtextfield 绑定 mysql 数据。所以我写了如下代码:

public class SimpleForm extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;
    Connection connection = null;
    Statement s = null;
    ResultSet rs;
    String tableName = "mytable";
    JTextField jtf1, jtf2;
    JButton jb;
    String selTable;
    public SimpleForm() {
        initComponents();
        DoConnect();
    }
    public void DoConnect() {
        try {
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/myschema", "root", "root");
            s = connection.createStatement();
            DatabaseMetaData dbm = connection.getMetaData();
            String[] types = { "TABLE" };
            rs = dbm.getTables(null, null, "%", types);
            selTable = "SELECT * FROM " + tableName;
        } catch (SQLException e) {
            System.out.println("Oops! Error occured..");
            e.printStackTrace();
        } finally {
            try {
                s.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    private void initComponents() {
        JPanel jp = new JPanel(new FlowLayout());
        JLabel jl1 = new JLabel("ID");
        jtf1 = new JTextField();
        jtf1.addActionListener(this);
        JLabel jl2 = new JLabel("Name");
        jtf2 = new JTextField();
        jtf2.addActionListener(this);
        jb = new JButton("Next");
        jb.addActionListener(this);
        jp.add(jl1);
        jp.add(jtf1);
        jp.add(jl2);
        jp.add(jtf2);
        jp.add(jb);
        add(jp);
    }
    public void actionPerformed(ActionEvent e) {
        try {
            s.execute(selTable);
            rs = s.getResultSet();
            if ((rs != null) && (rs.next())) {
                rs.next();
                jtf1.setText(rs.getString(1));
                jtf2.setText(rs.getString(2));
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
    }
    public static void main(String[] args) {
        new SimpleForm().setVisible(true);
    }
    }

我在 eclipse indigo 和 mysql 数据库中做这个。我已经在 lib 文件夹中导入了驱动程序的 jar 文件。我仍然收到以下错误。

Oops! Error occured..
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/myschema
    at java.sql.DriverManager.getConnection(DriverManager.java:604)
    at java.sql.DriverManager.getConnection(DriverManager.java:221)
    at main.SimpleForm.DoConnect(SimpleForm.java:55)
    at main.SimpleForm.<init>(SimpleForm.java:46)
    at main.SimpleForm.main(SimpleForm.java:135)
Exception in thread "main" java.lang.NullPointerException
    at main.SimpleForm.DoConnect(SimpleForm.java:74)
    at main.SimpleForm.<init>(SimpleForm.java:46)
    at main.SimpleForm.main(SimpleForm.java:135)

所以请帮我解决这个问题。我真的不明白这个问题。

【问题讨论】:

  • 检查 API 文档:您需要告诉 DriverManager 哪个是驱动程序。尝试使用 DriverManager.getDrivers() 检查您加载的内容。
  • 您必须使用构建路径在 Eclipse 中为 mysql-connector.jar 设置类路径,并且您必须将 mysql-connector 放入您的 web-app- 的 WEB-INF 目录的 lib 文件夹中(如果您正在开发任何网络应用程序)。
  • 哪个Java版本+驱动版本?如果您使用 Java 5 或更早版本,或者不兼容 JDBC 4.0 的旧驱动程序,则需要使用 Class.forName("com.mysql.jdbc.Driver") 显式加载驱动程序
  • @MarkRotteveel :使用所有最新的东西。因为我是java新手。所以下载了所有新东西。

标签: mysql eclipse jdbc


【解决方案1】:

你应该试试这个:

-您必须使用构建路径在 Eclipse 中为 mysql-connector.jar 设置类路径,并且您必须将 mysql-connector 放到您的 web-app- 的 WEB-INF 目录的 lib 文件夹中(如果您正在开发任何网络应用程序)。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-30
    • 2014-10-24
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    • 2016-09-07
    相关资源
    最近更新 更多