繁体   English   中英

mysql连接的JDBC驱动程序错误

[英]JDBC Driver error for mysql connection

我想用简单的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)

所以请帮我解决这个问题。 我真的没有问题。

您应该尝试这样:

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

希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM