Skip to content

Commit 56a9923

Browse files
Update QuestionTwo.java
1 parent 11dd43d commit 56a9923

File tree

1 file changed

+56
-61
lines changed

1 file changed

+56
-61
lines changed

QuestionTwo.java

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,64 @@
11
package Task9;
22

33
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
45
import java.sql.Statement;
56

6-
77
public class QuestionTwo {
8-
public static void main(String[] args) {
9-
Connection connection = MyDatabaseConnection.getConnection();
10-
try {
11-
Statement statement = connection.createStatement();
12-
String insertQuery = "create table employee(empcode int, empname varchar(255),emppage int, empsalary int)";
13-
System.out.println("Table Created......");
14-
int executeUpdate = statement.executeUpdate(insertQuery);
15-
if(executeUpdate > 0) {
16-
System.out.println("Record Inserted Successfully");
17-
}else {
18-
System.out.println("No Record placed yet......");
19-
}
20-
String insertQuery1 = "insert into employee value(101, 'Jenny', 25 , 10000)";
21-
int executeUpdate1 = statement.executeUpdate(insertQuery1);
22-
if(executeUpdate1 > 0) {
23-
System.out.println("Record 1 Inserted Successfully");
24-
}else {
25-
System.out.println("Internal Server Error");
26-
}
27-
String insertQuery2 = "insert into employee value(102, 'Jacky', 30 , 20000)";
28-
int executeUpdate2 = statement.executeUpdate(insertQuery2);
29-
if(executeUpdate2 > 0) {
30-
System.out.println("Record 2 Inserted Successfully");
31-
}else {
32-
System.out.println("Internal Server Error");
33-
}
34-
String insertQuery3 = "insert into employee value(103, 'Joe', 20 , 40000)";
35-
int executeUpdate3 = statement.executeUpdate(insertQuery3);
36-
if(executeUpdate3 > 0) {
37-
System.out.println("Record 3 Inserted Successfully");
38-
}else {
39-
System.out.println("Internal Server Error");
40-
}
41-
String insertQuery4 = "insert into employee value(104, 'John', 40 , 80000)";
42-
int executeUpdate4 = statement.executeUpdate(insertQuery4);
43-
if(executeUpdate4 > 0) {
44-
System.out.println("Record 4 Inserted Successfully");
45-
}else {
46-
System.out.println("Internal Server Error");
47-
}
48-
String insertQuery5 = "insert into employee value(105, 'Shameer', 25 , 90000)";
49-
int executeUpdate5 = statement.executeUpdate(insertQuery5);
50-
if(executeUpdate5 > 0) {
51-
System.out.println("Record 5 Inserted Successfully");
52-
}else {
53-
System.out.println("Internal Server Error");
54-
}
55-
statement.close();
56-
}catch(Exception e) {
57-
e.printStackTrace();
58-
}finally {
59-
try {
60-
connection.close();
61-
}catch(Exception e) {
62-
e.printStackTrace();
63-
}
64-
}
65-
66-
}
678

68-
}
9+
public static void main(String[] args) {
10+
11+
try (Connection connection = MyDatabaseConnection.getConnection()) {
12+
13+
if (connection == null) {
14+
System.out.println("❌ Database connection failed. Exiting...");
15+
return;
16+
}
17+
18+
// STEP 1: Create the table safely
19+
String createTableQuery = """
20+
CREATE TABLE IF NOT EXISTS employee (
21+
empcode INT PRIMARY KEY,
22+
empname VARCHAR(255),
23+
emppage INT,
24+
empsalary INT
25+
)
26+
""";
27+
28+
try (Statement statement = connection.createStatement()) {
29+
statement.execute(createTableQuery);
30+
System.out.println("✔ Table checked or created successfully.");
31+
}
32+
33+
// STEP 2: Insert employee records using PreparedStatement
34+
String insertQuery =
35+
"INSERT INTO employee (empcode, empname, emppage, empsalary) VALUES (?, ?, ?, ?)";
6936

37+
Object[][] employeeData = {
38+
{101, "Jenny", 25, 10000},
39+
{102, "Jacky", 30, 20000},
40+
{103, "Joe", 20, 40000},
41+
{104, "John", 40, 80000},
42+
{105, "Shameer", 25, 90000}
43+
};
44+
45+
try (PreparedStatement ps = connection.prepareStatement(insertQuery)) {
46+
47+
for (Object[] record : employeeData) {
48+
ps.setInt(1, (int) record[0]);
49+
ps.setString(2, (String) record[1]);
50+
ps.setInt(3, (int) record[2]);
51+
ps.setInt(4, (int) record[3]);
52+
53+
ps.executeUpdate();
54+
}
55+
56+
System.out.println("✔ All employee records inserted successfully.");
57+
}
58+
59+
} catch (Exception e) {
60+
System.err.println("❌ Error while inserting records:");
61+
e.printStackTrace();
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)