无法通过java执行创建表sql查询

esa*_*hmo 2 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
    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();
}
Run Code Online (Sandbox Code Playgroud)

kav*_*mre 5

这就是问题stmt.executeUpdate(sql);

使用execute(String SQL ) 方法代替executeUpdate 。

execute(String SQL)用于DDL/DML语句,而executeUpdate(String SQL)仅用于DML操作