Guest User

Untitled

a guest
Aug 14th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. package connection;
  2.  
  3. import java.sql.*;
  4.  
  5. public class DataBaseConnection {
  6. private Connection connection;
  7. private Statement statment;
  8. private ResultSet resultSet;
  9. private String sql;
  10.  
  11. public DataBaseConnection() {
  12. };
  13.  
  14. public boolean IsConnected() {
  15. String driver = "com.mysql.jdbc.Driver";
  16. String url = "jdbc:mysql://localhost/employes";
  17. String username = "root";
  18. String password = "";
  19.  
  20. try {
  21. Class.forName(driver).newInstance();
  22. } catch (InstantiationException e) {
  23. e.printStackTrace();
  24. return false;
  25.  
  26. } catch (IllegalAccessException e) {
  27. // TODO Auto-generated catch block
  28. e.printStackTrace();
  29. return false;
  30.  
  31. } catch (ClassNotFoundException e) {
  32. // TODO Auto-generated catch block
  33. e.printStackTrace();
  34. return false;
  35. }
  36. try {
  37. connection = DriverManager.getConnection(url, username, password);
  38. System.out.println("Connected");
  39.  
  40. } catch (SQLException e1) {
  41.  
  42. e1.printStackTrace();
  43. return false;
  44. }
  45. return true;
  46. }
  47.  
  48. public boolean executerUpdate(String sql) {
  49. try {
  50. statment = connection.createStatement();
  51. statment.executeUpdate(sql);
  52. } catch (SQLException e) {
  53.  
  54. e.printStackTrace();
  55. return false;
  56. }
  57.  
  58. return true;
  59. }
  60.  
  61. public boolean executerRequete(String sql) {
  62. try {
  63. statment = connection.createStatement();
  64. setResultSet(statment.executeQuery(sql));
  65. } catch (SQLException e) {
  66. // TODO Auto-generated catch block
  67. e.printStackTrace();
  68. return false;
  69. }
  70. return true;
  71. }
  72.  
  73. /**
  74. * @param resultSet
  75. * the resultSet to set
  76. */
  77. public void setResultSet(ResultSet resultSet) {
  78. this.resultSet = resultSet;
  79. }
  80.  
  81. /**
  82. * @return the resultSet
  83. */
  84. public ResultSet getResultSet() {
  85. return resultSet;
  86. }
  87.  
  88. /**
  89. * @param sql
  90. * the sql to set
  91. */
  92. public void setSql(String sql) {
  93. this.sql = sql;
  94. }
  95.  
  96. /**
  97. * @return the sql
  98. */
  99. public String getSql() {
  100. return sql;
  101. }
  102. }
Add Comment
Please, Sign In to add comment