-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateTableExample2.java
More file actions
74 lines (60 loc) · 2.44 KB
/
Copy pathCreateTableExample2.java
File metadata and controls
74 lines (60 loc) · 2.44 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.sql.*;
import java.util.Scanner;
public class CreateTableExample2 {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
// 1. Load Driver and connect
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/AJT",
"root",
"YashPujara@18"
);
// 2. Create Statement
Statement stmt = conn.createStatement();
// 3. Create Table
String createTableSQL = "CREATE TABLE IF NOT EXISTS Emp (" +
"Id INT PRIMARY KEY, " +
"Name VARCHAR(50), " +
"City VARCHAR(50), " +
"Age INT)";
stmt.executeUpdate(createTableSQL);
System.out.println("Table 'Emp' is ready.");
/*
// 4. Prepare Insert
String insertSQL = "INSERT INTO Emp (Id, Name, City, Age) VALUES (?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(insertSQL);
for (int i = 1; i <= 3; i++) {
System.out.println("\nEnter details for Employee " + i + ":");
System.out.print("ID: ");
int empId = sc.nextInt();
sc.nextLine();
System.out.print("Name: ");
String empName = sc.nextLine();
System.out.print("City: ");
String empCity = sc.nextLine();
System.out.print("Age: ");
int age = sc.nextInt();
pstmt.setInt(1, empId);
pstmt.setString(2, empName);
pstmt.setString(3, empCity);
pstmt.setInt(4, age);
pstmt.executeUpdate();
System.out.println("Employee " + i + " inserted successfully.");
}*/
// 5. Call Stored Procedure
System.out.print("\nEnter an Employee ID to get City using Stored Procedure: ");
int searchId = sc.nextInt();
CallableStatement cstmt = conn.prepareCall("{call In_OutSP(?, ?, ?, ?)}");
cstmt.setInt(1,sc.nextInt());
cstmt.execute();
String cityResult = cstmt.getString(3);
System.out.println("City of Employee with ID " + cityResult);
// 6. Close resources
cstmt.close();
//pstmt.close();
stmt.close();
conn.close();
sc.close();
}
}