表不是在javamySQL中创建的
我使用mySQL workbench创建了一个名为testdb的数据库。然后我通过数据库连接到它>;连接到数据库。然后我用java(eclipse)编写一些代码来
- 获取到数据库的连接
- 在数据库中创建一个表李>
代码的输出显示连接和创建表已经完成
但是当我回到mySQL时,我在数据库中没有看到任何表。有人能帮我解释一下为什么不创建表吗
package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class main {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
getConnection();
createTable();
}
public static void createTable() throws Exception {
try {
Connection con = getConnection();
PreparedStatement create = con.prepareStatement("CREATE TABLE IF NOT EXISTS tablename(id int NOT NULL AUTO_INCREMENT, first varchar(255), last varchar(255), PRIMARY KEY(id)");
create.executeUpdate();
}catch(Exception e) {System.out.println(e);}
finally {
System.out.println("Function complete.");
};
}
public static Connection getConnection() throws Exception{
try {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/testdb";
String username = "root";
String password = "00000";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
System.out.println("Connected");
return conn;
} catch(Exception e) {System.out.println(e);}
return null;
}
}
以下是输出:
连接的
连接的
爪哇。sql。SQLSyntaxErrorException:您的SQL语法有错误;请查看与MySQL服务器版本对应的手册,以获取第1行“”附近使用的正确语法
功能完成
# 1 楼答案
sql语句中缺少括号
)
,正确的是