Guest User

Untitled

a guest
Feb 16th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. package com.gces.jdbc;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.sql.*;
  6.  
  7. public class Demo extends Frame
  8. {
  9. // DECLARATIONS FOR DATABASE PART
  10. public static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
  11. public static final String DB_URL = "jdbc:mysql://localhost:3306/book_registration";
  12. public static final String UNAME = "root";
  13. public static final String PASSWORD = "";
  14. String insertQuery;
  15.  
  16. // for GUI PART
  17. Label bookNameLabel, authorNameLabel;
  18. TextField bookTextField, authorTextField;
  19. Button saveButton;
  20.  
  21. Demo()
  22. {
  23. prepareGUI();
  24. }
  25. // main function
  26. public static void main(String[] args)
  27. {
  28. Demo obj = new Demo();
  29. obj.dataBaseFunction();
  30. }
  31. public void prepareGUI()
  32. {
  33. this.setTitle("Book Registration System");
  34. this.setSize(500,500);
  35. this.setVisible(true);
  36. this.setLayout(new FlowLayout());
  37. // closable frame
  38. this.addWindowListener(new WindowAdapter()
  39. {
  40. @Override public void windowClosing(WindowEvent we)
  41. {
  42. System.exit(0);
  43. }
  44. });
  45. bookNameLabel = new Label("Book:");
  46. bookTextField = new TextField("Enter the name of Book",50);
  47. authorNameLabel = new Label("Author:");
  48. authorTextField = new TextField("Enter the name of author",50);
  49. saveButton = new Button("SAVE");
  50.  
  51. this.add(bookNameLabel);
  52. this.add(bookTextField);
  53. this.add(authorNameLabel);
  54. this.add(authorTextField);
  55. this.add(saveButton);
  56.  
  57.  
  58. // when user focus on textFields the initial text should be gone. This is for Keyboard focus
  59. bookTextField.addFocusListener(new FocusListener()
  60. {
  61. public void focusGained(FocusEvent f)
  62. {
  63. bookTextField.setText("");
  64. }
  65.  
  66. public void focusLost(FocusEvent f)
  67. {
  68. if(bookTextField.getText().equals(""))
  69. bookTextField.setText("Enter the name of book");
  70. }
  71. });
  72. authorTextField.addFocusListener(new FocusListener()
  73. {
  74. public void focusGained(FocusEvent f)
  75. {
  76. authorTextField.setText("");
  77. }
  78.  
  79. public void focusLost(FocusEvent f)
  80. {
  81. if(authorTextField.getText().equals(""))
  82. authorTextField.setText("Enter the name of author");
  83. }
  84. });
  85. }
  86. //database roles comes when user enter the data and click save button
  87. public void dataBaseFunction()
  88. {
  89. saveButton.addActionListener(new ActionListener()
  90. {
  91. @Override public void actionPerformed(ActionEvent e)
  92. {
  93. try
  94. {
  95. insertQuery = "INSERT INTO `books_table`(`book_name`,`author_name`)"+"VALUES(?,?)";
  96.  
  97. Class.forName(JDBC_DRIVER);
  98.  
  99. Connection con = DriverManager.getConnection(DB_URL,UNAME,PASSWORD);
  100.  
  101. PreparedStatement pst = con.prepareStatement(insertQuery);
  102. pst.setString(1,bookTextField.getText());
  103. pst.setString(2,authorTextField.getText());
  104. pst.executeUpdate();
  105.  
  106. pst.close();
  107. con.close();
  108. }
  109. catch (ClassNotFoundException e1) {
  110. e1.printStackTrace();
  111. } catch (SQLException e1) {
  112. e1.printStackTrace();
  113. }
  114. }
  115. });
  116. }
  117.  
  118. }
Add Comment
Please, Sign In to add comment