-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsert.java
34 lines (32 loc) · 1.34 KB
/
Insert.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
import java.sql.*;
public class Insert {
public static void main(String args[]) throws ClassNotFoundException {
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "root";
String password = "System@321sql";
String query = "INSERT INTO employees(id, name, job, salary) VALUES (6, 'Mohit', 'web Designer', 760000);";
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 st = con.createStatement();
int rowsaffected = st.executeUpdate(query);
if(rowsaffected>0) {
System.out.println("Insertion Successfully"+rowsaffected+ "row(s) affected.");
}else {
System.out.println("Insertion Failed..");
}
st.close();
con.close();
System.out.println();
System.out.println("connection closed successfully!!!");
} catch(SQLException e) {
System.out.println(e.getMessage());
}
}
}