使用java在mysql数据库中删除表

时间:2015-01-02 03:12:05

标签: java mysql database

我想从我的mysql数据库中删除一个表,我正在使用这个函数:

注意:user,pass和database是变量,我100%它们是正确的,因为我在其他正常工作的函数中使用它们。

public static void deleteTable(String name) {
        String myDriver = "org.gjt.mm.mysql.Driver";
        String myUrl = "jdbc:mysql://108.167.188.13:3306/" + database;
        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(myUrl, user, pass);
            stmt = conn.createStatement();
            String sql = "CREATE DATABASE " + name;
            stmt.executeUpdate(sql);
        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) {
                    conn.close();
                }
            } catch (SQLException se) {

            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException se) {
                se.printStackTrace();
            }//end finally try
        }//end try

    }

我收到以下错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user 'adren248_0'@'%' to database 'last_data_20_18_1_1_2015_TESTE'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2788)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1816)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1730)
    at cleandb.CleanDB.deleteTable(CleanDB.java:65)
    at cleandb.CleanDB.main(CleanDB.java:34)

我无法理解这个错误,我确定我已经正确设置了权限。看:

http://imgur.com/rLbCMse

2 个答案:

答案 0 :(得分:0)

您的代码声明您正在尝试创建数据库而不是删除表。也许添加更改查询语句将为您完成工作

答案 1 :(得分:-1)

stmt.execute(“ DROP TABLE your_table_name;”);

基本SQL 101