/src/penjualantoko/Connecting.java

https://gitlab.com/qoriah/programpenjualan · Java · 120 lines · 89 code · 9 blank · 22 comment · 5 complexity · 8fa6229cdc823ae282e1e496269bb4de MD5 · raw file

  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package penjualantoko;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JTable;
  13. import javax.swing.table.DefaultTableModel;
  14. /**
  15. *
  16. * @author Tika
  17. */
  18. public class Connecting {
  19. public Connection konek;
  20. public DefaultTableModel dtm;
  21. public ResultSet rst;
  22. public boolean OpenCnt() {
  23. try {
  24. Class.forName("com.mysql.jdbc.Driver");
  25. String host = "localhost";
  26. String port = "3306";
  27. String db = "java2";
  28. String username = "root";
  29. String password = "";
  30. konek = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + db, username, password);
  31. return true;
  32. } catch (ClassNotFoundException ex) {
  33. JOptionPane.showConfirmDialog(null, "Class not found");
  34. return false;
  35. } catch (SQLException ex) {
  36. JOptionPane.showMessageDialog(null, "Failed To Connect To database");
  37. return false;
  38. }
  39. /**
  40. * this method is used to open a connection to database with a Defined
  41. * host, port , database, username, and password
  42. */
  43. }
  44. public boolean OpenCnt(String host, String port, String db, String username, String password) {
  45. try {
  46. Class.forName("com.mysql.jdbc.Driver");
  47. konek = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + db, username, password);
  48. return true;
  49. } catch (ClassNotFoundException ex) {
  50. JOptionPane.showConfirmDialog(null, "Class not found");
  51. return true;
  52. } catch (SQLException ex) {
  53. JOptionPane.showMessageDialog(null, "Failed To Connect To database");
  54. return false;
  55. }
  56. /**
  57. * this method is used to open a connection to database with a
  58. * variability host, port , database, username, and password
  59. */
  60. }
  61. public boolean execute(String SQL) {
  62. try {
  63. // TODO add your handling code here:
  64. Statement stmt = konek.createStatement();
  65. stmt.executeUpdate(SQL);
  66. return true;
  67. } catch (SQLException ex) {
  68. JOptionPane.showMessageDialog(null, "Failed To Execute Query");
  69. return false;
  70. }
  71. /**
  72. * this method is used to execute a DML Query type this method can't
  73. * execute a DDL Query type
  74. */
  75. }
  76. public boolean execute(String SQL, JTable TabelContainerSwing, String[] ColumnName) {
  77. dtm = new DefaultTableModel(null, ColumnName) {
  78. @Override
  79. public boolean isCellEditable(int row, int column) {
  80. return false;
  81. }
  82. };
  83. try {
  84. Statement stmt = konek.createStatement();
  85. //System.out.println(SQ);
  86. rst = stmt.executeQuery(SQL);
  87. if (rst == null) {
  88. JOptionPane.showMessageDialog(null, "Data Not Found");
  89. TabelContainerSwing.setModel(dtm);
  90. return true;
  91. } else {
  92. String[] data = new String[ColumnName.length];
  93. while (rst.next()) {
  94. int i = ColumnName.length - 1;
  95. while (i >= 0) {
  96. System.out.println(rst.getString(ColumnName[i]));
  97. data[i] = rst.getString(ColumnName[i]);
  98. i--;
  99. }
  100. dtm.addRow(data);
  101. }
  102. TabelContainerSwing.setModel(dtm);
  103. return true;
  104. }
  105. } catch (SQLException ex) {
  106. JOptionPane.showMessageDialog(null, "Failed to retrieve data error on SQL or Column did not match the database");
  107. TabelContainerSwing.setModel(dtm);
  108. return true;
  109. }
  110. }
  111. }