-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsert.java
More file actions
42 lines (41 loc) · 1.67 KB
/
Insert.java
File metadata and controls
42 lines (41 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.*;
import java.sql.*;
class Insert {
public static void main(String args[]) {
try {
int emp, y, count = 0;
String na, desg, nat;
double sal;
Scanner scan = new Scanner(System.in);
System.out.println("Enter empid:");
emp = scan.nextInt();
System.out.println("Enter year of joining");
y = scan.nextInt();
System.out.println("Enter name:");
na = scan.nextLine();
System.out.println("Enter designation:");
desg = scan.nextLine();
System.out.println("Enter nationality:");
nat = scan.nextLine();
System.out.println("Enter salary:");
sal = scan.nextInt();
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/company", "root", "root");
Statement stm = con.createStatement();
String qry = "insert into employee(empid,name,designation,nationality,year,salary)values(" + emp + ",'" + na
+ "','" + desg + "','" + nat + "'," + y + "," + sal + ")";
count = stm.executeUpdate(qry);
if (count == 1) {
System.out.println("Data Inserted");
} else {
System.out.println("Data not inserted");
}
stm.close();
con.close();
} catch (SQLException e) {
System.out.println("Caught Exception:" + e);
} catch (ClassNotFoundException e) {
System.out.println("Caught Exception:" + e);
}
}
}