无法通过java执行Create table sql查询

无法通过java执行Create table sql查询,java,mysql,sql,database,Java,Mysql,Sql,Database,我试图通过java字符串创建表,但它显示错误,因为表不存在,但当我直接在工作台上运行相同的查询时,它运行良好。下面是我的代码 String url = "jdbc:mysql://localhost:3306/" ; String dbname = "tweetmap"; String username = "root"; String password = "root"; try { // SQL Driver needed for connecting to Database

我试图通过java字符串创建表,但它显示错误,因为表不存在,但当我直接在工作台上运行相同的查询时,它运行良好。下面是我的代码

String url = "jdbc:mysql://localhost:3306/" ;
String dbname = "tweetmap";
String username = "root";
String password = "root";
try
{ 
    // SQL Driver needed for connecting to Database
    Class.forName("com.mysql.jdbc.Driver");
    c = DriverManager.getConnection(url+dbname,username,password);
    c.setAutoCommit(true);
    stmt = c.createStatement();

    //Creating the Database if not Already Present
    String sql = "CREATE TABLE if not exists senti "
            + "( latitude double NULL, "
            + "longitude double NULL, "
            +  "Sentiment TEXT  NULL) ";
    stmt.executeUpdate(sql);

    if(sentiment != null){

        stmt1 = c.createStatement();    
        stmt1.executeUpdate("INSERT INTO `senti`(latitude,longitude,Sentiment) VALUE ('"+lati+"','"+longi+"','"+sentiment+"')");
        }
    }
catch(Exception e){
     e.printStackTrace();
}
这就是问题所在stmt.executeUpdatesql

不要使用executeUpdate,而是使用executeString SQL方法

executeString SQL用于DDL/DML语句
虽然executeUpdateString SQL仅用于DML操作,但在JDBC中执行任何查询的最佳方法是使用execute方法。此方法可用于任何类型的查询。 我希望下面的链接能帮助你了解更多


您是否遇到任何异常?我认为是bcoz u没有为表指定主键&从查询中删除IF NOT EXISTS子句。尝试相同的方法。请参阅下面的链接。愿这对你有帮助。我只是根据本教程编写的,问题是当我直接在workbench上运行查询时,它工作正常,并且我的表中没有需要主键签出的属性