2012-09-29 92 views
0

我在運行此代碼時發生錯誤。我不知道爲什麼。我只是想調用一個被調用的方法將數據存儲在MySQL數據庫上。 下面的代碼:如何在MySQL上插入Java數據

public Employee(String firstName, String lastName, 
       String userID, String userPassword, String role) 
{ 
    try{ 
     Class.forName("com.mysql.jdbc.Driver"); 
     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotelmanagementsystem",       "some_user",""); 

     PreparedStatement statement = con.prepareStatement("Insert into employee(Firstname, Lastname, UserID, UserPassword, Role)values(?, ?, ?, ?, ?");  
     statement.setString(1, firstName); 
     statement.setString(2, lastName); 
     statement.setString(3, userID); 
     statement.setString(4, userPassword); 
     statement.setString(5, role); 
     statement.executeUpdate(); 

     con.close(); 
    } 
    catch(Exception e) { 
     e.printStackTrace();   
     return; 
    } 
} 

控制檯:語法錯誤

回答

4

變化

PreparedStatement statement = con.prepareStatement("Insert into employee(Firstname, Lastname, UserID, UserPassword, Role)values(?, ?, ?, ?, ?"); 

PreparedStatement statement = con.prepareStatement("Insert into employee(Firstname, Lastname, UserID, UserPassword, Role)values(?, ?, ?, ?, ?)"); 

你失蹤右括號)

+0

@juergend,我沒有解釋它說「你錯過了關閉支架)」 –

+0

沒有得到更新。 –

1

你忘記了值的關閉paranthesis(。

0

向您的查詢添加一個雙引號,然後它會沒事的。

PreparedStatement statement = con.prepareStatement("Insert into employee(Firstname, Lastname, UserID, UserPassword, Role)values(?, ?, ?, ?, ?)");  
1
PreparedStatement statement = con.prepareStatement("Insert into employee(Firstname, Lastname, UserID, UserPassword, Role)values(?, ?, ?, ?, ?");  

應該

PreparedStatement statement = con.prepareStatement("Insert into employee(Firstname, Lastname, UserID, UserPassword, Role)values(?, ?, ?, ?, ?)"); 

你缺少右括號。