java按钮poening mysql连接无法正常工作

时间:2015-12-19 05:40:12

标签: java event-handling

我试图通过点击按钮执行查询,但我似乎有些不对劲。

btn.setOnAction(new EventHandler<ActionEvent>() {


    public void handle(ActionEvent e) {

        try {
            String host = "jdbc:mysql://localhost:3306/fabius";
            String uName = "x";
            String uPass = "x";

            Connection con = DriverManager.getConnection(host, uName, uPass);

            Statement stmt = con.createStatement();
            String SQL = "SELECT * from prices";
            ResultSet rs = stmt.executeQuery(SQL);

            System.out.println(rs);
        }
        catch (SQLException err) {
            System.out.println(err.getMessage());
        }

    }
});

这会返回错误

no suitable driver found

1 个答案:

答案 0 :(得分:0)

不知何故,这可以按要求运作。

btn.setOnAction(new EventHandler<ActionEvent>() {



    public void handle(ActionEvent e) {
       try{
       String host = "jdbc:mysql://localhost:3306/fabius";
       String uName = "root";
       String uPass = "cupcup";
       Connection con = DriverManager.getConnection(host, uName, uPass);

       Statement stmt = con.createStatement();
       String SQL = "SELECT `HGM.L` from prices";
       ResultSet rs = stmt.executeQuery(SQL);


       rs.next();
       System.out.println(rs.getInt(1));

       }

       catch (SQLException err) {
            System.out.println(err.getMessage());
        }


    }
});