Skip to content

Commit

Permalink
Create README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
AkashKobal authored Feb 18, 2024
1 parent 8f595a9 commit 066a92f
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions insert_data_into_database/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Insert data into database using JDBC.
```
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");
}
}
```

0 comments on commit 066a92f

Please sign in to comment.