-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJDBCInsertRecord.java
53 lines (49 loc) · 1.4 KB
/
JDBCInsertRecord.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package dbms;
import java.sql.*;
public class JDBCInsertRecord {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB = "jdbc:mysql://localhost:3306/student";
// 1. Database User name & Password
static final String USER = "admin";
static final String PWD = "sjcet123";
public static void main(String[] args) {
Connection con = null;
Statement stmt = null;
try {
// 2. Register JDBC driver
Class.forName(JDBC_DRIVER);
// 3. Open a connection
System.out.println("Connecting to database...");
con = DriverManager.getConnection(DB, USER, PWD);
// 4. Execute a query
System.out.println("Inserting record..");
stmt = con.createStatement();
String sql = "INSERT INTO user values"
+ "(2,'Sam','George',23),"
+"(3,'Anil','Pate',21),"
+"(1,'Ram','Naik',20)";
int i = stmt.executeUpdate(sql);
System.out.println("Rows inserted: "+i);
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
catch (Exception e) {
System.out.println(e.getMessage());
}
finally {
try {
System.out.println("Closing Connection");
if (stmt != null)
stmt.close();
if (con != null)
con.close();
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
}
System.out.println("Ending Program..");
}
}