-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentDatabaseConnection.java
More file actions
36 lines (30 loc) · 1.41 KB
/
Copy pathStudentDatabaseConnection.java
File metadata and controls
36 lines (30 loc) · 1.41 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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class StudentDatabaseConnection {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/java_students", "root", "");
System.out.println("\nConnection string is " + conn.toString());
System.out.println("\nHow many records you want to enter into the table: ");
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
for (int i = 1; i <= r; i++) {
Scanner scname = new Scanner(System.in);
Scanner scrollno = new Scanner(System.in);
System.out.println("\nEnter the name for (" + i + ") record: ");
String inpName = scname.nextLine();
System.out.println("Enter the rollno for (" + i + ") record: ");
int inpRollno = scrollno.nextInt();
String name = "\"" + inpName + "\"";
int rollno = inpRollno;
String query = "INSERT into students VALUES (" + name + "," + rollno + ");";
System.out.println(i + " Query is :" + query);
Statement st = conn.createStatement();
int val = st.executeUpdate(query);
System.out.println(i + " record added");
}
conn.close();
}
}