Advertisement
Guest User

Untitled

a guest
Feb 26th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. import com.teamincredibles.settings.Settings;
  9.  
  10. public class Database {
  11.  
  12. private Connection connection;
  13.  
  14. public void connect() {
  15. try {
  16. Class.forName(Settings.getDatabaseDriver());
  17. connection = DriverManager.getConnection(Settings.getJdbcUrl(), Settings.getDBUserName(), Settings.getDBPassword());
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. }
  21. }
  22.  
  23. public boolean prepareStatement(String query, String choice1) {
  24. boolean flag = false;
  25. try {
  26. PreparedStatement ps = connection.prepareStatement(query);
  27. ps.setString(1, choice1);
  28. ResultSet rs = ps.executeQuery();
  29. flag = rs.next();
  30. } catch (SQLException e) {
  31. e.printStackTrace();
  32. }
  33. return flag;
  34. }
  35.  
  36. public boolean prepareStatement(String query, String choice1, String choice2) {
  37. boolean flag = false;
  38. try {
  39. PreparedStatement ps = connection.prepareStatement(query);
  40. ps.setString(1, choice1);
  41. ps.setString(2, choice2);
  42. ResultSet rs = ps.executeQuery();
  43. flag = rs.next();
  44. } catch (SQLException e) {
  45. e.printStackTrace();
  46. }
  47. return flag;
  48. }
  49.  
  50. public void executeUpdate(String query) {
  51. try {
  52. Statement statement = connection.createStatement();
  53. statement.executeUpdate(query);
  54. } catch (SQLException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58.  
  59. public boolean execute(String query) {
  60. boolean result = false;
  61. try {
  62. Statement statement = connection.createStatement();
  63. result = statement.execute(query);
  64. } catch (SQLException e) {
  65. e.printStackTrace();
  66. }
  67. return result;
  68. }
  69.  
  70. public ResultSet executeQuery(String query) {
  71. ResultSet result = null;
  72. try {
  73. Statement statement = connection.createStatement();
  74. result = statement.executeQuery(query);
  75. } catch (SQLException e) {
  76. e.printStackTrace();
  77. }
  78. return result;
  79. }
  80.  
  81. public void disconnect() {
  82. try {
  83. if (connection != null) {
  84. connection.close();
  85. }
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement