3 回答

TA貢獻1829條經驗 獲得超4個贊
您尚未加載數據庫驅動程序,要執行此操作,請包含以下代碼:
Class.forName("com.mysql.jdbc.Driver");
如果您還沒有驅動程序下載并放入項目庫。

TA貢獻1828條經驗 獲得超3個贊
他們是這個代碼片段的一些問題。一是你沒有加載數據庫。你也沒有使用 username 和 password。
我建議您單獨創建數據庫連接。也許在一個單獨的 Java 文件中。如下,
public class DatabaseConnection {
public static Statement getConnection() throws Exception{
Class.forName("com.mysql.jdbc.Driver"); //Loading the database
Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3307/restaurentsystem","nandika","nandika"); //username and password can save as variables and pass here
Statement statement = c.createStatement();
return statement;
}
}
然后你可以隨時使用它。在這種情況下,
try {
Statement s = DatabaseConnection.getConnection();
s.executeUpdate("INSERT INTO addbook (Book ID, Book Name, Author, Publisher) VALUES (?, ?, ?, ?);"); //Values should assign here.
System.out.println("updated");
} catch (Exception e) {
System.out.println(e);
}
如果我這樣做,這就是我所做的。我建議你試試這個方法。
添加回答
舉報