Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. package DB;
  2.  
  3.  
  4.  
  5.  
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12.  
  13. /*
  14. * To change this license header, choose License Headers in Project Properties.
  15. * To change this template file, choose Tools | Templates
  16. * and open the template in the editor.
  17. */
  18.  
  19. /**
  20. *
  21. * @author nilsf
  22. */
  23. public class DataBase {
  24. static Connection con;
  25.  
  26. public static Connection getCon()
  27. {
  28. try {
  29. if(con==null){
  30. Class.forName("com.mysql.jdbc.Driver");
  31. con=DriverManager.getConnection("jdbc:mysql://localhost:3306/prog","root","bjerk7412395");
  32. }
  33. }catch (ClassNotFoundException ex) {
  34. Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
  35. } catch (SQLException ex) {
  36. Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
  37. }
  38.  
  39. return con;
  40. }
  41. public static void CloseConnection()
  42. {
  43. if(con!=null)
  44. {
  45. try
  46. {
  47. con.close();
  48. con = null;
  49. }
  50. catch (SQLException ex)
  51. {
  52. ex.printStackTrace();
  53. }
  54. }
  55.  
  56. }
  57.  
  58. public static ResultSet getResultFromSqlQuery(String SqlQueryString)
  59. {
  60. System.out.println("in funcation");
  61. ResultSet rs=null;
  62. if(con==null)
  63. {
  64. getCon();
  65. }
  66. try
  67. {
  68. rs = con.createStatement().executeQuery(SqlQueryString);
  69. }
  70. catch (SQLException ex)
  71. {
  72. ex.printStackTrace();
  73. }
  74. return rs;
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement