Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. package bookDatabase;
  2.  
  3.  
  4. public class Book {
  5. String bookName; // Create Variable for the book name//
  6. String authorName;
  7. String price;
  8.  
  9. //We now create the setters and getters
  10. public Book( String bookName, String authorName, String price){
  11. this.bookName = bookName;
  12. this.authorName = authorName;
  13. this.price = price;
  14. }
  15.  
  16. public void setbookName (String bookName ){
  17. this.bookName = bookName;
  18. }
  19.  
  20. public void setauthorName (String authorName){
  21. this.authorName = authorName;
  22. }
  23.  
  24. public void setprice (String price){
  25. this.price = price;
  26. }
  27.  
  28. public String getbookName(){
  29. return bookName;
  30. }
  31.  
  32. public String getauthorName (){
  33. return authorName;
  34. }
  35.  
  36. public String getprice (){
  37. return price;
  38. }
  39.  
  40.  
  41.  
  42. @Override
  43. public String toString(){
  44. return bookName + authorName + price ;
  45. }
  46.  
  47. }
  48.  
  49. package bookDatabase;
  50.  
  51. import java.awt.*;
  52.  
  53. import javax.swing.*;
  54. import java.awt.event.ActionEvent;
  55. import java.awt.event.ActionListener;
  56. import java.sql.SQLException;
  57.  
  58. public class guiBook extends JFrame{
  59.  
  60. private String bookName;
  61. private String authorName;
  62. private String price;
  63. Book b1 = new Book(bookName, authorName, price);
  64. BookQuerie q1 = new BookQuerie(bookName, authorName, price);
  65. private JTabbedPane pane = new JTabbedPane();
  66. private JPanel panel1 = new JPanel();
  67. private JTextArea areaShow= new JTextArea("List of All Books");
  68. private JTable selectArea = new JTable();
  69.  
  70. private JPanel panel2 = new JPanel();
  71. private JLabel update1 = new JLabel ("Insert Name of Book", JLabel.LEFT);
  72. private JTextField updateField1 = new JTextField(20);
  73. private JLabel update2 = new JLabel ("Enter the new Price", JLabel.LEFT);
  74. private JTextField updateField2 = new JTextField(20);
  75. private JButton updateButon = new JButton("Update price");
  76.  
  77.  
  78. private JPanel panel3 = new JPanel();
  79. private JTextField firstInsert = new JTextField(20);
  80. private JTextField secondInsert = new JTextField(20);
  81. private JTextField thirdInsert = new JTextField(20);
  82. private JButton insertButton = new JButton("Insert New Book");
  83. private JLabel labelInsert1 = new JLabel("Enter Book Name", JLabel.LEFT);
  84. private JLabel labelInsert2 = new JLabel("Enter Author Name", JLabel.LEFT);
  85. private JLabel labelInsert3 = new JLabel("Enter Price");
  86.  
  87. private JPanel panel4 = new JPanel();
  88. private JTextField firstDelete = new JTextField(20);
  89. private JButton buttonDelete = new JButton("Delete Book");
  90. private JLabel labelDelete = new JLabel ( "Enter Name of Book", JLabel.LEFT);
  91.  
  92.  
  93. public guiBook() throws Exception{
  94. pane.add("Display All Books", panel1);
  95. panel1.add(areaShow, BorderLayout.WEST);
  96. areaShow.setEditable(false);
  97. panel1.add(selectArea, BorderLayout.EAST);
  98.  
  99.  
  100.  
  101. {
  102.  
  103. pane.add("Update Book Price", panel2);
  104. panel2.add(update1);
  105. panel2.add(updateField1);
  106. panel2.add(update2);
  107. panel2.add(updateField2);
  108. panel2.add(updateButon);
  109. updateButon.addActionListener(new ActionListener(){
  110. @Override
  111. public void actionPerformed(ActionEvent e){
  112. String bookName = updateField1.getText();
  113. String price = updateField2.getText();
  114. try {
  115. BookQuerie.updateBooks(bookName, price);
  116. }
  117. catch (SQLException e1) {
  118. e1.printStackTrace();
  119. }
  120. }
  121. });
  122.  
  123. pane.add("Insert New Book",panel3);
  124. panel3.add(labelInsert1);
  125. panel3.add(firstInsert);
  126. panel3.add(labelInsert2);
  127. panel3.add(secondInsert);
  128. panel3.add(labelInsert3);
  129. panel3.add(thirdInsert);
  130. panel3.add(insertButton);
  131. insertButton.addActionListener(new ActionListener(){
  132. @Override
  133. public void actionPerformed(ActionEvent e){
  134. String bookName = firstInsert.getText();
  135. String authorName = secondInsert.getText();
  136. String price = thirdInsert.getText();
  137. try {
  138. BookQuerie.insertBooks(bookName,authorName, price);
  139. }
  140. catch (SQLException e1) {
  141. e1.printStackTrace();
  142. }
  143. }
  144. });
  145.  
  146.  
  147. pane.add("Delete Book", panel4);
  148. panel4.add(labelDelete);
  149. panel4.add(firstDelete);
  150. panel4.add(buttonDelete);
  151. buttonDelete.addActionListener(new ActionListener(){
  152. @Override
  153. public void actionPerformed(ActionEvent e){
  154. String bookName = firstDelete.getText();
  155. try {
  156. BookQuerie.deleteBooks(bookName);
  157. }
  158. catch (SQLException e1) {
  159. e1.printStackTrace();
  160. }
  161. }
  162. });
  163.  
  164. add(pane);
  165. }
  166. }
  167.  
  168.  
  169. public static void main(String[] args) throws Exception{
  170. guiBook app = new guiBook();
  171. app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  172. app.setSize(1000,400);
  173. app.setVisible(true);
  174. }
  175.  
  176.  
  177. }
  178.  
  179. package bookDatabase;
  180.  
  181. import java.sql.*;
  182.  
  183. import javax.swing.JOptionPane;
  184. public class BookQuerie {
  185. private static PreparedStatement selectAll;
  186. private static PreparedStatement updateBook;
  187. private static PreparedStatement insertBook;
  188. private static PreparedStatement deleteBook;
  189. private Connection connect;
  190. private String bookName;
  191. private static String authorName;
  192. private static String price;
  193. Book boooks = new Book(bookName, authorName, price);
  194.  
  195.  
  196. public BookQuerie(String bookName, String authorName, String price) {
  197. try{
  198. connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookshop", "root", "");
  199. selectAll = connect.prepareStatement("SELECT * FROM BOOK");
  200. updateBook = connect.prepareStatement("UPDATE book SET price = ? WHERE bookName = ?");
  201. insertBook = connect.prepareStatement("INSERT INTO book VALUES (?, ?, ?)");
  202. deleteBook= connect.prepareStatement("DELETE FROM book WHERE bookName = ? ");
  203. }
  204. catch
  205. (SQLException ex){
  206. ex.printStackTrace();
  207. }
  208. }
  209.  
  210. public static String selectAll(String bookName, String authorName, String price) throws SQLException{
  211. ResultSet rs = selectAll.executeQuery();
  212. while(rs.next()){
  213. rs.getString ("bookName");
  214. rs.getString ("authorName");
  215. rs.getString ("price");
  216. }
  217. return selectAll(bookName, authorName, price);
  218. }
  219.  
  220. public static int updateBooks(String bookName, String price) throws SQLException{
  221. updateBook.setString(1, bookName );
  222. updateBook.setString(2, price);
  223. int i = updateBook.executeUpdate();
  224. if(i>0){
  225. JOptionPane.showMessageDialog(null, "Data Is Saved!!");
  226. System.exit(0);
  227. }
  228. else{
  229. JOptionPane.showMessageDialog(null, "Data is Not Saved! :( " );
  230. System.exit(0);
  231. }
  232. return updateBooks(bookName, price);
  233. }
  234.  
  235. public static int insertBooks(String bookName, String authorName, String price) throws SQLException{
  236. insertBook.setString(1, bookName);
  237. insertBook.setString(2,authorName);
  238. insertBook.setString(3, price);
  239. int i = insertBook.executeUpdate();
  240. if(i>0){
  241. JOptionPane.showMessageDialog(null, "Data Is Saved!!");
  242. }
  243. else{
  244. JOptionPane.showMessageDialog(null, "Data is Not Saved! :( " );
  245. }
  246. return insertBooks(bookName, authorName, price );
  247. }
  248.  
  249. public static int deleteBooks (String bookName) throws SQLException{
  250. deleteBook.setString(1, bookName);
  251. return deleteBook.executeUpdate();
  252. }
  253.  
  254.  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement