-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddStudent.java
44 lines (35 loc) · 1 KB
/
AddStudent.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
36
37
38
39
40
41
42
43
44
package JDBC_ManageStudent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class AddStudent {
public void add() {
Student student = new Student();
// Enter value
Scanner sc = new Scanner(System.in);
System.out.println("Enter name: ");
student.setName(sc.nextLine());
System.out.println("Enter age: ");
student.setAge(sc.nextInt());
sc.close();
// Connect to table
ConnectJDBC connectJdbc = new ConnectJDBC();
Connection connection = connectJdbc.getconnect();
String query = "INSERT INTO student(name,age)" + "VALUES (?,?)";
PreparedStatement ppst = null;
// Add value to table
try {
ppst = connection.prepareStatement(query);
ppst.setString(1, student.getName());
ppst.setInt(2, student.getAge());
int row = ppst.executeUpdate();
if (row != 0) {
System.out.println("Add sucess " + row +" row(s)");
}
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}