-
Notifications
You must be signed in to change notification settings - Fork 0
/
Insert.java
35 lines (33 loc) · 1.15 KB
/
Insert.java
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
package task1;
import java.sql.*;
import java.util.Scanner;
public class Insert {
Insert(String url, String username, String password){
try {
Scanner sc = new Scanner(System.in);
Connection con = DriverManager.getConnection(url, username, password);
String insertQuery = "INSERT INTO Students (student_id, first_name, last_name, grade) VALUES (?, ?, ?, ?)";
PreparedStatement insertStmt = con.prepareStatement(insertQuery);
// Set the values for the insert query
System.out.println("Enter id: ");
int id = sc.nextInt();
System.out.println("Enter first name: ");
String fname = sc.next();
System.out.println("Enter last name: ");
String lname = sc.next();
System.out.println("Enter grade: ");
int grade = sc.nextInt();
insertStmt.setInt(1, id); // student_id
insertStmt.setString(2, fname); // first_name
insertStmt.setString(3, lname); // last_name
insertStmt.setInt(4, grade); // grade
// Execute the insert query
insertStmt.executeUpdate();
Read r = new Read(url, username, password);
con.close();
sc.close();
}catch(Exception e) {
System.out.println(e);
}
}
}