10
10
import java .sql .SQLException ;
11
11
import java .sql .Statement ;
12
12
13
+ import java .text .DateFormat ;
14
+ import java .text .SimpleDateFormat ;
15
+
16
+ import java .util .Date ;
13
17
import java .util .Enumeration ;
14
18
import java .util .Properties ;
15
19
@@ -25,11 +29,22 @@ public class Program {
25
29
26
30
private static final String DB_TABLE = "animals" ;
27
31
32
+ private static final String DB_UPDATE_COLUMN = "remark" ;
33
+
28
34
private static final String DB_COLUMN = "id" ;
29
35
private static final int DB_COLUMN_VALUE = 1 ;
30
36
31
37
private static final String DB_TOTAL_NAME = "total" ;
32
38
39
+ private static String getNow ()
40
+ {
41
+ Date date = new Date ();
42
+ DateFormat dateFormat = new SimpleDateFormat ("d.M.yyyy H:mm:ss" );
43
+ String retValue = dateFormat .format (date );
44
+
45
+ return retValue ;
46
+ }
47
+
33
48
public static void main (String [] args ) {
34
49
try {
35
50
Class .forName (DB_DRIVER );
@@ -47,6 +62,16 @@ public static void main(String[] args) {
47
62
out .println (key + " : " + value );
48
63
}
49
64
65
+ // UPDATE statement
66
+ String new_comment = "Java " + getNow ();
67
+
68
+ PreparedStatement stm0 = conn .prepareStatement ("update " + DB_TABLE + " set " + DB_UPDATE_COLUMN + "=? where " + DB_COLUMN + "!=?" );
69
+ stm0 .setString (1 , new_comment );
70
+ stm0 .setInt (2 , DB_COLUMN_VALUE );
71
+ int updatedRows0 = stm0 .executeUpdate ();
72
+ out .println ("Total updated rows: " + updatedRows0 );
73
+ out .println ();
74
+
50
75
// Full SELECT statement
51
76
Statement stm1 = conn .createStatement ();
52
77
ResultSet rs1 = stm1 .executeQuery ("select * from " + DB_TABLE );
0 commit comments