/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import java.sql.*; 

2. import java.io.*; 

3. public class JDBC2 

4.

5.   public static void 

6.   main ( String[] args ) 

7.   { 

8.     Connection con  = null

9.     Statement stm = null

10.     try

11.     Class.forName("com.mysql.jdbc.Driver") ; 

12.  

13.     con = DriverManager.getConnection 

14.         ( "jdbc:mysql://localhost/test" ); 

15.  

16.     stm = con.createStatement 

17.         ( ResultSet.TYPE_SCROLL_INSENSITIVE

18.           ResultSet.CONCUR_UPDATABLE ); 

19.  

20. //    ResultSet.TYPE_SCROLL_SENSITIVE 

21. //      ResultSet.CONCUR_READ_ONLY 

22.  

23.     ResultSet rs = stm.executeQuery 

24.         ("SELECT * FROM members ORDER BY ID" ); 

25.  

26.     int count = 0

27.     if ( rs.last() )  count = rs.getRow() ; 

28.     System.out.println ( "Number of rows : " + count ); 

29.  

30.     String nm ; 

31.     int id ; 

32.     boolean ok = rs.first() ; 

33.     if ( ok ) 

34.     while ( ! rs.isAfterLast()  ) 

35.     { 

36.       id = rs.getInt ("id"); 

37.       nm = rs.getString ("lname"); 

38.       System.out.println ( id + " " + nm ); 

39.       rs.next() ; 

40.     } 

41. System.out.println ( "----------------------" ); 

42.  

43.     ok =  rs.last() ; 

44.     if ( ok ) 

45.     for ( int i = count ; 

46.            i >= 1 ;    i-- ) 

47.     { 

48.       id = rs.getInt ("id"); 

49.       nm = rs.getString ("lname"); 

50.  

51.       if ( nm.equals( "urnip" ) ) 

52.       { 

53.         rs.updateString( "lname", "Turnip" ) ; 

54.         rs.updateRow() ; 

55.         nm = "Turnip"

56.       } 

57.       System.out.println ( id + " " + nm ); 

58.       rs.previous() ; 

59.     } 

60. System.out.println ( "+++++++++++++++++++++++" ); 

61.  

62.     rs.beforeFirst(); 

63.     while ( rs.next() ) 

64.     { 

65.       System.out.println( 

66.        rs.getRow()  + " " 

67.        + rs.getInt(1) + " "

68.        rs.getString(2) + " " + rs.getString(3) ); 

69.     } 

70.     } 

71.     catch ( Exception ex ) { 

72.       System.err.println ("Error : " + ex ); 

73.     } 

74.     finally { 

75.       try

76.         if ( stm != null

77.           stm.close (); 

78.       } catch ( Exception e ) {} 

79.       try

80.         if ( con != null

81.           con.close (); 

82.       }  catch ( Exception e ) {} 

83.     } 

84.   } 

85.

86. /* 

87. mysql> select * from members; 

88. +----+----------+------------+------+ 

89. | id | fname    | lname      | fee  | 

90. +----+----------+------------+------+ 

91. |  5 | Giorgio  | Delmonte   |   25

92. | 10 | Jennifer | Zappo      |   14

93. | 15 | Koichi   | Mitsumatsu |   60

94. | 20 | Baldruck | urnip      |   44

95. +----+----------+------------+------+ 

96. 4 rows in set (0.39 sec) 

97.  

98. Output

99. Number of rows : 4 

100. 5 Delmonte 

101. 10 Zappo 

102. 15 Mitsumatsu 

103. 20 urnip 

104. ---------------------- 

105. 20 Turnip 

106. 15 Mitsumatsu 

107. 10 Zappo 

108. 5 Delmonte 

109. +++++++++++++++++++++++ 

110. 1 5 Giorgio Delmonte 

111. 2 10 Jennifer Zappo 

112. 3 15 Koichi Mitsumatsu 

113. 4 20 Baldruck Turnip 

114. ================== 

115. rs.moveToInsertRow(); 

116. rs.updateInt( "id", 155 ); 

117. rs.updateString( "lname", "Flipstone" ); 

118. ... 

119. rs.insertRow(); 

120. ================== 

121. rs.absolute( 51 ); 

122. rs.relative( -1 ); 

123. rs.deleteRow(); 

124. ================== 

125. DatabaseMetaData dmd = con.getMetaData() ; 

126. if ( dmd.othersUpdatesAreVisible( ResultSet.TYPE_SCROLL_SENSITIVE)) 

127. if ( dmd.othersInsertsAreVisible( ... 

128. if ( dmd.othersDeletesAreVisible( ... 

129. */ 

130.