我使用 mySQL workbench 創(chuàng)建了一個名為 testdb 的數(shù)據(jù)庫。然后我通過數(shù)據(jù)庫>連接到數(shù)據(jù)庫連接到它。然后我用java(eclipse)寫了一些代碼來獲取數(shù)據(jù)庫連接在數(shù)據(jù)庫中創(chuàng)建一個表。代碼的輸出顯示連接和創(chuàng)建表已完成。但是當(dāng)我返回 mySQL 時,我在數(shù)據(jù)庫中看不到任何表。誰能幫我解釋為什么沒有創(chuàng)建表?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; }}這是輸出:連接的連接的java.sql.SQLSyntaxErrorException:你的 SQL 語法有錯誤;檢查與您的 MySQL 服務(wù)器版本對應(yīng)的手冊,了解在第 1 行的 '' 附近使用的正確語法功能齊全。
1 回答

至尊寶的傳說
TA貢獻1789條經(jīng)驗 獲得超10個贊
你的sql語句中少了一個括號)
,正確的是
"CREATE TABLE IF NOT EXISTS tablename(id int NOT NULL AUTO_INCREMENT, first varchar(255), last varchar(255), PRIMARY KEY(id))"
添加回答
舉報
0/150
提交
取消