-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion of data.java
More file actions
35 lines (32 loc) · 1.33 KB
/
Copy pathInsertion of data.java
File metadata and controls
35 lines (32 loc) · 1.33 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
import java.sql.*;
public class Main {
public static void main(String[] args) throws ClassNotFoundException {
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "root";
String password = "#Reyazlove1";
String query = "INSERT INTO employee(id, name, job_title, salary) VALUES(3, 'Harshit', 'Full Stack Developer',87000.0);";
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded successfully");
}catch (ClassNotFoundException e){
System.out.println(e.getMessage());
}
try{
Connection con = DriverManager.getConnection(url, username , password);
System.out.println("connection Established successfully");
Statement stmt = con.createStatement();
int rowsaffected = stmt.executeUpdate(query);
if(rowsaffected>0){
System.out.println("Insert Successfully. "+rowsaffected+" row(s) affected.");
}else{
System.out.println("Insertion failed");
}
stmt.close();
con.close();
System.out.println();
System.out.println("connection closed successfully");
}catch (SQLException e){
System.out.println(e.getMessage());
}
}
}