-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJDBCExampleP1.java
99 lines (83 loc) · 2.99 KB
/
JDBCExampleP1.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package demo;
// prepared stmt
//STEP 1. Import required packages
import java.sql.*;
public class JDBCExampleP1 {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3308/students";
// Database credentials
static final String USER = "root";
static final String PASS = "1234";
public static void main(String[] args) {
Connection conn = null;
PreparedStatement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
String sql = "INSERT INTO reg (id,first,last,age)" +
"VALUES (?,?,?,?)";
stmt = conn.prepareStatement(sql);
//Bind values into the parameters.
stmt.setInt(1, 104); // This would set age
stmt.setString(2,"sagar"); // This would set ID
stmt.setString(3,"patel");
stmt.setInt(4,23);
int row= stmt.executeUpdate();
System.out.println("no of rows impact:"+row);
stmt.setInt(1, 105); // This would set age
stmt.setString(2,"hinal"); // This would set ID
stmt.setString(3, "makwana");
stmt.setInt(4,22);
row= stmt.executeUpdate();
System.out.println("no of rows impact:"+row);
// Let us update age of the record with ID = 102;
// Let us select all the records and display them.
sql = "SELECT id, first, last, age FROM reg";
stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
//STEP 5: Extract data from result set
while(rs.next()){
//Retrieve by column name
int id = rs.getInt(1);
int age = rs.getInt(4);
String first = rs.getString(2);
String last = rs.getString(3);
//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end JDBCExample