|
| Advance Java | IndianTechnoEra |
Aim:
Write a java program to enter employee details in an employee table with attributes- ID, name and salary using JDBC.
Process:
Step 1: Load the dirver Class.forName("com.mysql.jdbc.Driver");
Step 2: Creating a connection
String url="jdbc:mysql://localhost:3306/class_db";
String username="root";
String password="mysql1234";
Connection con=DriverManager.getConnection(url,username,password);
Step 3: Create a query
String sql="Insert into employee(id,name,gender,designation,salary) values(?,?,?,?,?)";
Step 4: Get the preparedStatement object:
PreparedStatement ps=con.prepareStatement(sql);
Step 5: Set the value of query
ps.setInt(1, id);
ps.setString(2,name);
ps.setString(3,gender);
ps.setString(4, designation);
ps.setFloat(5, salary);
ps.executeUpdate();
Step 6: Close the statement and connection:
ps.close();
con.close();
Input Code:
Output:
![]() |
| Java code to Enter Employee Details in Database |

