Question
I have used this script and I created a table in mysql CREATE TABLE `employee` ( `id` int(3) NOT NULL AUTO_INCREMENT, `first_name` varchar(20) DEFAULT NULL,
I have used this script and I created a table in mysql
CREATE TABLE `employee` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`first_name` varchar(20) DEFAULT NULL,
`last_name` varchar(20) DEFAULT NULL,
`username` varchar(250) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
`address` varchar(45) DEFAULT NULL,
`contact` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
And this is my class to connect mysql and get the values from the user
public class EmployeeDao {
public int registerEmployee(Employee employee) throws ClassNotFoundException {
String INSERT_USERS_SQL = "INSERT INTO employee" +
" (id, first_name, last_name, username, password, address, contact) VALUES " +
" (?, ?, ?, ?, ?,?,?);";
int result = 0;
Class.forName("com.mysql.cj.jdbc.Driver");
try (Connection connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/employees?allowPublicKeyRetrieval=true&useSSL=false", "root", "root");
// Step 2:Create a statement using connection object
PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USERS_SQL)) {
preparedStatement.setInt(1, 1);
preparedStatement.setString(2, employee.getFirstName());
preparedStatement.setString(3, employee.getLastName());
preparedStatement.setString(4, employee.getUsername());
preparedStatement.setString(5, employee.getPassword());
preparedStatement.setString(6, employee.getAddress());
preparedStatement.setString(7, employee.getContact());
System.out.println("Successful");
System.out.println(preparedStatement);
// Step 3: Execute the query or update query
result = preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println("fail");
// process sql exception
printSQLException(e);
}
return result;
}
Mysql suppose to generate unique Primary key for every row but I can insert (first_name,last_name,username,password,address,contact ) for first row and when i tried second I'm getting Duplicate entry '1' for key 'employee.PRIMARY'. I though AUTO_INCREMENT generate unique PK for every row. I would appreciate for help.
This is how the user enter values:
Employee Register Form First Name Last Name UserName Password Address Contact No SubmitStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started