Guest User

Untitled

a guest
Jun 3rd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1. //mycotroller...
  2. package fxapplication;
  3.  
  4. import java.net.URL;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ResourceBundle;
  11. import javafx.event.ActionEvent;
  12. import javafx.fxml.FXML;
  13. import javafx.fxml.Initializable;
  14. import javafx.scene.control.Button;
  15. import javafx.scene.control.ComboBox;
  16. import javafx.scene.control.TextField;
  17. import javax.swing.JOptionPane;
  18.  
  19. /**
  20. * FXML Controller class
  21. *
  22. * @author PHILSERVER
  23. */
  24. public class SearchController implements Initializable {
  25.  
  26. @FXML
  27. private Connection con;
  28.  
  29. @FXML
  30. private Statement stmt;
  31.  
  32. @FXML
  33. private ResultSet rs;
  34.  
  35. @FXML
  36. private ComboBox cbGender;
  37.  
  38. @FXML
  39. private TextField tfLocation;
  40.  
  41. @FXML
  42. private TextField tfFirstname;
  43.  
  44. @FXML
  45. private TextField tfSearch;
  46.  
  47. @FXML
  48. private Button update;
  49.  
  50. @FXML
  51. private Button insert;
  52.  
  53. @FXML
  54. private Button delete;
  55.  
  56. @FXML
  57. private TextField tfSecondname;
  58.  
  59. @FXML
  60. void search(ActionEvent event) {
  61.  
  62. if (tfSearch.getText().equals("")){
  63. JOptionPane.showMessageDialog(null," Enter firstname please !!!");
  64. }else{
  65. if(checkName()){
  66. search();
  67.  
  68. }else{
  69. JOptionPane.showMessageDialog(null,"firstname does not exist");
  70. }
  71.  
  72. }
  73.  
  74. }
  75.  
  76.  
  77. @FXML
  78. void deleteRecord(ActionEvent event) {
  79. try {
  80. Class.forName("com.mysql.jdbc.Driver");
  81. } catch(ClassNotFoundException e) {
  82. JOptionPane.showMessageDialog(null,"Unable to register class "+e.getMessage());
  83. }
  84.  
  85. try {
  86. con = DriverManager.getConnection("jdbc:mysql://localhost/level2","root","addison");
  87. stmt = con.createStatement();
  88. } catch (SQLException e) {
  89. JOptionPane.showMessageDialog(null,"Unable to connect to database "+e.getMessage());
  90. }
  91.  
  92. try{
  93. String sql;
  94. sql="delete from sstudent where firstname='"+tfSearch.getText()+"'";
  95. int result=stmt.executeUpdate(sql);
  96. JOptionPane.showMessageDialog(null," Record deleted sucessfully");
  97. tfSearch.setText(null);
  98. tfFirstname.setText(null);
  99. tfSecondname.setText(null);
  100. cbGender.setValue(null);
  101. tfLocation.setText(null);
  102.  
  103. } catch (SQLException e) {
  104. JOptionPane.showMessageDialog(null,"Unable to delete record "+e.getMessage());
  105. }
  106. }
  107.  
  108. @FXML
  109. void updateRecord(ActionEvent event) {
  110. try {
  111. Class.forName("com.mysql.jdbc.Driver");
  112. } catch(ClassNotFoundException e) {
  113. JOptionPane.showMessageDialog(null,"Unable to register class "+e.getMessage());
  114. }
  115.  
  116. try {
  117. con = DriverManager.getConnection("jdbc:mysql://localhost/level2","root","addison");
  118. stmt = con.createStatement();
  119. } catch (SQLException e) {
  120. JOptionPane.showMessageDialog(null,"Unable to connect to database "+e.getMessage());
  121. }
  122.  
  123.  
  124. try{
  125. String sql;
  126. sql="update sstudent set secondname='"+tfSecondname.getText()+"',"
  127. +"location='"+tfLocation.getText()+"',"
  128. +"gender='"+cbGender.getValue().toString()+"'"
  129. +" where firstname='"+tfSearch.getText()+"'";
  130.  
  131.  
  132. int result=stmt.executeUpdate(sql);
  133. JOptionPane.showMessageDialog(null," Record updated sucessfully");
  134. } catch (SQLException f) {
  135. JOptionPane.showMessageDialog(null,"Unable to update record "+f.getMessage());
  136. }
  137.  
  138.  
  139. }
  140.  
  141. @FXML
  142. void insertRecord(ActionEvent event) {
  143. try {
  144. Class.forName("com.mysql.jdbc.Driver");
  145. } catch(ClassNotFoundException e) {
  146. JOptionPane.showMessageDialog(null,"Unable to register class "+e.getMessage());
  147. }
  148.  
  149. try {
  150. con = DriverManager.getConnection("jdbc:mysql://localhost/level2","root","addison");
  151. stmt=con.createStatement();
  152. }catch (SQLException e){
  153. JOptionPane.showMessageDialog(null,"Unable to connect to database "+e.getMessage());
  154. }
  155.  
  156.  
  157. try {
  158. String sql;
  159. sql="insert into sstudent(firstname,secondname,gender,location) values ("
  160. +"'"+tfFirstname.getText()+"',"
  161. +"'"+tfSecondname.getText()+"',"
  162. +"'"+cbGender.getValue().toString()+"',"
  163. +"'"+tfLocation.getText()+"')";
  164.  
  165. int result=stmt.executeUpdate(sql);
  166. JOptionPane.showMessageDialog(null,"record saved sucessfully ");
  167. } catch (Exception e) {
  168. JOptionPane.showMessageDialog(null,"Unable to insert record "+e.getMessage());
  169. }
  170.  
  171.  
  172. }
  173.  
  174.  
  175. private boolean checkName(){
  176. try {
  177. Class.forName("com.mysql.jdbc.Driver");
  178. } catch(ClassNotFoundException e) {
  179. JOptionPane.showMessageDialog(null,"Unable to register class "+e.getMessage());
  180. }
  181.  
  182. try {
  183. con = DriverManager.getConnection("jdbc:mysql://localhost/level2","root","addison");
  184. stmt = con.createStatement();
  185. } catch (SQLException e) {
  186. JOptionPane.showMessageDialog(null,"Unable to connect to database "+e.getMessage());
  187. }
  188. try{
  189. String sql;
  190. sql="select * from sstudent where firstname='"+tfSearch.getText()+"'";
  191. rs=stmt.executeQuery(sql);
  192. if(rs.next()){
  193. return true;
  194. }
  195. }catch (SQLException e){
  196. JOptionPane.showMessageDialog(null,"unable to retrieve record"+ e.getMessage());
  197. }
  198. return false;
  199. }
  200.  
  201. private void search(){
  202. try {
  203. Class.forName("com.mysql.jdbc.Driver");
  204. } catch(ClassNotFoundException e) {
  205. JOptionPane.showMessageDialog(null,"Unable to register class "+e.getMessage());
  206. }
  207.  
  208. try {
  209. con = DriverManager.getConnection("jdbc:mysql://localhost/level2","root","addison");
  210. stmt = con.createStatement();
  211. } catch (SQLException e) {
  212. JOptionPane.showMessageDialog(null,"Unable to connect to database "+e.getMessage());
  213. }
  214.  
  215. try {
  216. String sql;
  217. sql = "select * from sstudent where firstname='"+tfSearch.getText()+"'";
  218. rs = stmt.executeQuery(sql);
  219. while(rs.next()){
  220. tfFirstname.setText(rs.getString("firstname"));
  221. tfSecondname.setText(rs.getString("secondname"));
  222. cbGender.setValue(rs.getString("gender"));
  223. tfLocation.setText(rs.getString("location"));
  224. }
  225. } catch (SQLException e) {
  226. JOptionPane.showMessageDialog(null,"Unable to retrieve record "+e.getMessage());
  227. }
  228. }
  229. @Override
  230. public void initialize(URL url, ResourceBundle rb) {
  231.  
  232.  
  233.  
  234. cbGender.getItems().addAll("male","female");
  235. }
  236.  
  237. }
  238.  
  239. //main class....
  240. package fxapplication;
  241.  
  242. import javafx.application.Application;
  243. import javafx.fxml.FXMLLoader;
  244. import javafx.scene.Parent;
  245. import javafx.scene.Scene;
  246. import javafx.scene.media.Media;
  247. import javafx.scene.media.MediaPlayer;
  248. import javafx.stage.Stage;
  249.  
  250. /**
  251. *
  252. * @author PHILSERVER
  253. */
  254. public class FXApplication extends Application {
  255.  
  256. @Override
  257. public void start(Stage stage) throws Exception {
  258. Parent root = FXMLLoader.load(getClass().getResource("search.fxml"));
  259.  
  260. Scene scene = new Scene(root);
  261.  
  262. stage.setScene(scene);
  263.  
  264. stage.show();
  265.  
  266. // my scene builder forms....
Add Comment
Please, Sign In to add comment