0

运行此代码时出现错误。我不知道为什么。我只想做一个方法,当被调用时会将数据存储在 MySQL 数据库中。这是代码:

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

4 回答 4

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(?, ?, ?, ?, ?)");

你错过了右括号)

于 2012-09-29T10:38:08.237 回答
1

您忘记了 values(.

于 2012-09-29T10:39:15.150 回答
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(?, ?, ?, ?, ?)");

你缺少右括号。

于 2012-09-29T10:45:28.767 回答
0

在您的查询中添加双引号,然后就可以了。

    PreparedStatement statement = con.prepareStatement("Insert into employee(Firstname, Lastname, UserID, UserPassword, Role)values(?, ?, ?, ?, ?)");    
于 2012-09-29T10:41:22.640 回答