-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinsertData.java
37 lines (26 loc) · 1.25 KB
/
insertData.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
package insertData;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;
public class insertData {
private static Scanner sc;
public static void main(String[] args) throws Exception { // throws Exception is used to handle the exception without using try-catch block
sc = new Scanner(System.in);
System.out.println("Enter the student details: ");
System.out.print("Enter the student id: ");
int id = sc.nextInt();
System.out.print("Enter the student name: ");
String name = sc.next();
System.out.print("Enter the student branch: ");
String branch = sc.next();
Class.forName("com.mysql.cj.jdbc.Driver"); // load and register the driver
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3308/jdbc_db", "root", "Akash@123"); // establish the connection
PreparedStatement ps = connection.prepareStatement("insert into student values('"+id+"','"+name+"','"+branch+"')"); // execute the query")
int i = ps.executeUpdate(); // execute the query
if (i > 0) // if the record is inserted successfully then it will return 1 else 0
System.out.println("Record inserted successfully");
else
System.out.println("Record not inserted successfully");
}
}