-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJdbcSelectTest_insert.java
36 lines (29 loc) · 1.49 KB
/
JdbcSelectTest_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
35
36
import java.sql.*; // Use 'Connection', 'Statement' and 'ResultSet' classes in java.sql package
// JDK 1.7 and above
public class JdbcSelectTest_insert { // Save as "JdbcSelectTest.java"
public static void main(String[] args) {
try (
// Step 1: Allocate a database 'Connection' object
Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/IUT_DB?autoReconnect=true&useSSL=false", "root", "1234");
// MySQL: "jdbc:mysql://hostname:port/databaseName", "username", "password"
// Step 2: Allocate a 'Statement' object in the Connection
Statement stmt = conn.createStatement();) {
// Step 3: Execute a SQL SELECT query, the query result
// is returned in a 'ResultSet' object.
int sid=999;
String sname="Muhaamad";
String strSelect = "insert into student values (" +sid+","+"'" +sname+"')";
System.out.println("The SQL query is: " + strSelect); // Echo For debugging
System.out.println();
int count = stmt.executeUpdate(strSelect);
// Step 4: Process the ResultSet by scrolling the cursor forward via next().
// For each row, retrieve the contents of the cells with getXxx(columnName).
conn.close();
} catch(SQLException ex) {
ex.printStackTrace();
}
// Step 5: Close the resources - Done automatically by try-with-resources
}
}
// to delete record
// String strSelect = "delete from student where studentid ="+ sid;