-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBC-Connection.java
More file actions
55 lines (42 loc) · 2.07 KB
/
Copy pathJDBC-Connection.java
File metadata and controls
55 lines (42 loc) · 2.07 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.sql.*;
public class Main{
//create connection ....
private static final String url = "jdbc:mysql://127.0.0.1:3306/MyLocalDataBase";
private static final String username = "root";
private static final String password = "Paras@12345";
public static void main(String[] args) {
try{ // Load Drivers ......
Class.forName("com.mysql.cj.jdbc.Driver");
} catch(ClassNotFoundException e){
System.out.println(e.getMessage());
}
try{
Connection connection = DriverManager.getConnection(url , username, password); // create connection ...
Statement statement = connection.createStatement(); // create statement ..
String query = String.format("INSERT INTO Students (name , age, marks) VALUES('%s' , %d , %f)", "manoj" , 67 , 89.4);
// this line is used to insert data into existing table ...
// String query = "select * from Students" ; // query to print and take data from database .....
//
// ResultSet resultset= statement.executeQuery(query); // it is used when we want to retrive data .....
// statement.executeUpdate(); // It is used when we insert , delete or update data .....
// while(resultset.next()){
// int age = resultset.getInt("age");
// Double marks = resultset.getDouble("marks");
// String name = resultset.getString ("name");
//
// System.out.println("age :" + age);
//
// System.out.println("marks :" + marks);
// System.out.println("name :" + name);
// }
int rowsAffected = statement.executeUpdate (query);
if(rowsAffected>0){
System.out.println("data updated succesfully");
}else{
System.out.println("Data is not updated");
}
}catch(SQLException e){
System.out.println(e.getMessage());
}
}
}