/* 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.applet.Applet

3. import java.awt.*; 

4. import java.awt.event.*; 

5.  

6. public class JDBCPrepStat 

7.           extends Applet 

8.           implements ActionListener 

9.

10.   PreparedStatement pstm = null

11.   Connection con  = null

12.  

13.   public  void 

14.     init ( ) 

15.   { 

16.     try

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

18.  

19.     con = DriverManager.getConnection 

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

21.  

22.     pstm = con.prepareStatement 

23.      ( "INSERT INTO MEMBERS"

24.       "(ID, LNAME, FNAME, FEE)"

25.       "VALUES(?, ?, ?, ?)" 

26.       ); 

27.  

28.     add ( tfid ); 

29.     add ( tflnm ); 

30.     add ( tffnm ); 

31.     add ( tffee ); 

32.     add ( bt ); 

33.     bt.setEnabled ( true ); 

34.     bt.addActionListener ( this ); 

35.     add ( tfmsg ); 

36.     } 

37.     catch ( Exception ex ) { 

38.       tfmsg.setText ("Error : " + ex ); 

39.     } 

40.   } 

41.   public void actionPerformed(ActionEvent e) 

42.   { 

43.   ///NO Validation !! 

44.     try

45.       String str = tfid.getText() ; 

46.       pstm.setInt ( 1, Integer.parseInt( str ) ) ; 

47.       pstm.setString ( 2, tflnm.getText() ) ; 

48.       pstm.setString ( 3, tffnm.getText() ) ; 

49.       str = tffee.getText() ; 

50.       pstm.setInt ( 4, Integer.parseInt ( str ) ) ; 

51.  

52.       pstm.executeUpdate ( ); 

53.       tfmsg.setText ( "OK, added" ) ; 

54.     } 

55.     catch ( SQLException sex ) 

56.     { 

57.       tfmsg.setText ( "Error : " + sex ) ; 

58.     } 

59.     catch ( NumberFormatException nex ) 

60.     { 

61.       tfmsg.setText ( "Error : " + nex ) ; 

62.     } 

63.   } 

64.   public void destroy() 

65.   { 

66.     try

67.       if (con != null

68.         con.close (); 

69.     } 

70.     catch ( Exception e ) 

71.     { 

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

73.     } 

74.   } 

75.   Button bt = new Button ( "Make it so" ); 

76.   TextField tfid = new TextField ( 10 ); 

77.   TextField tflnm = new TextField ( 20 ); 

78.   TextField tffnm = new TextField ( 20 ); 

79.   TextField tffee = new TextField ( 20 ); 

80.   TextField tfmsg = new TextField ( 40 ); 

81.

82.