Advertisement
Guest User

Untitled

a guest
Apr 17th, 2017
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. //This class is able to be used in any project or with another class to connect with MySQL database.
  2. //Author -Dinushka95@yahoo.com
  3. //11/12/2016
  4. package MainSystem;
  5.  
  6. import com.mysql.jdbc.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13.  
  14.  
  15. public class AutoDB_Connect {
  16. //Thses varriables are the main varriables ued in this and other class that's why
  17. //it is public and Static because i wanted it to be in globle level
  18. public static Connection DB_connection =null;
  19. public static PreparedStatement DB_PreparedStatement =null;
  20. public static ResultSet DB_ResultSet =null;
  21.  
  22. //Default Contructor will use above valuse
  23. private static String DB_Host= "jdbc:mysql://localhost:3306/garmentsystem";
  24. private static String username="YOUR USERNAME";
  25. private static String password="YOUR PASSWORD";
  26.  
  27. //Default Contructor
  28. public AutoDB_Connect() {
  29.  
  30. }
  31.  
  32. // this constructor can be used to change your connection settings
  33. public AutoDB_Connect(String Database_URL,int Port,String DatabaseName,String Username,String Password) {
  34. DB_Host="jdbc:mysql://"+Database_URL+":"+Port+"/"+DatabaseName+"";
  35. username=Username;
  36. password=Password;
  37.  
  38. }
  39.  
  40. // used to start a connection with the above selected settings
  41. public void connect()
  42. {
  43. try
  44. {
  45. Class.forName("com.mysql.jdbc.Driver");
  46. DB_connection=(Connection) DriverManager.getConnection(DB_Host,username,password);
  47. System.out.println("Succesfully connected to "+DB_Host);
  48. }
  49. catch(Exception e)
  50. {
  51. System.out.println(e);
  52. }
  53. }
  54.  
  55. // Close the DB connection
  56. public void close()
  57. {
  58. try {
  59. DB_connection.close();
  60. } catch (SQLException ex) {
  61. Logger.getLogger(AutoDB_Connect.class.getName()).log(Level.SEVERE, null, ex);
  62. }
  63.  
  64. }
  65.  
  66. // this method is used to excute SQL queryies without a return value
  67. // this method will return a boolen value if it is sucessfull and a message in the console
  68. public boolean execute(String SQL_String){
  69.  
  70. DB_ResultSet =null;
  71. //System.out.println(SQL_String);
  72. try {
  73. DB_PreparedStatement =DB_connection.prepareStatement(SQL_String);
  74. DB_PreparedStatement.execute();
  75. System.out.println("***Successfull***Excuted "+SQL_String);
  76.  
  77. return true;
  78.  
  79. } catch (SQLException ex) {
  80. System.out.println("***Fail***Excuted "+SQL_String);
  81. Logger.getLogger(AutoDB_Connect.class.getName()).log(Level.SEVERE, null, ex);
  82.  
  83. return false;
  84. }
  85. }
  86.  
  87. // this method is used to excute SQL queryies with a return value
  88. // this method will return a Resultset if it is sucessfull Or NULL value if it fails and a message in the console
  89. public ResultSet executeQuery(String SQL_String)
  90. {
  91. DB_ResultSet =null;
  92. System.out.println(SQL_String);
  93. try {
  94. DB_PreparedStatement =DB_connection.prepareStatement(SQL_String);
  95. DB_ResultSet=DB_PreparedStatement.executeQuery();
  96. System.out.println("***Successfull***Excuted "+SQL_String);
  97.  
  98. return DB_ResultSet;
  99.  
  100. } catch (SQLException ex) {
  101. System.out.println("***Fail***Excuted "+SQL_String);
  102. Logger.getLogger(AutoDB_Connect.class.getName()).log(Level.SEVERE, null, ex);
  103. return null;
  104. }
  105.  
  106. }
  107.  
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement