-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMy_crud.java
More file actions
114 lines (79 loc) · 2.8 KB
/
My_crud.java
File metadata and controls
114 lines (79 loc) · 2.8 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.sql.*;
import java.util.*;
public class My_crud {
public static void main(String[] args) {
String JDBC_DRIVER="com.mysql.cj.jdbc.Driver";
String DB_URL="jdbc:mysql://localhost:3306/people_db";
String USER="root";
String PASS="";
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt= conn.createStatement();
String query="Create Table if not Exists users (Id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255), email VARCHAR(255), PRIMARY KEY (id))";
stmt.execute(query);
Scanner scan= new Scanner(System.in);
System.out.println("1. adicionar user ");
System.out.println("2. Verificar user");
System.out.println("3. update user");
System.out.println("4. detetar user");
System.out.println("Ecolha uma opcao");
String choice= scan.nextLine();
switch (choice) {
case "1":
System.out.println( "Enter user name: ");
String name=scan.nextLine();
System.out.println( "Enter user email: ");
String email=scan.nextLine();
query= "INSERT INTO users (name, email) VALUES ('"+name+"','"+email+"')";
stmt.executeUpdate(query);
break;
case "2":
//read user
System.out.println("enter user id: ");
int id= scan.nextInt();
query= "SELECT * FROM users WHERE id = "+ id;
ResultSet rs= stmt.executeQuery(query);
if(rs.next()) {
System.out.println("ID:"+rs.getInt("id"));
System.out.println("Name:"+rs.getString("name"));
System.out.println("Email:"+rs.getString("email"));
}else {
System.out.println("User not found!");
}
break;
case "3":
System.out.println("Enter user id: ");
id= scan.nextInt();
System.out.println( "Enter new user name: ");
name=scan.nextLine();
scan.nextLine();
System.out.println( "Enter new user email: ");
email=scan.nextLine();
query= "UPDATE users SET name = '"+name+"', email = '"+email+"' WHERE id= " + id;
int result= stmt.executeUpdate(query);
if(result> 0) {
System.out.println("user updated successfully" );
}else {
System.out.println("User not found !");
}
break;
case "4":
System.out.println("Enter user id: ");
id= scan.nextInt();
query= " DELETE FROM users WHERE id = " + id;
result= stmt.executeUpdate(query);
if(result> 0) {
System.out.println("user deleted successfully" );
}else {
System.out.println("User not found !");
}
break;
}
stmt.close();
conn.close();
} catch (Exception e) {
System.out.println("Erro:"+e.getMessage());
}
}
}