Guest User

Untitled

a guest
Jul 15th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. package DB;
  2. import java.sql.*;
  3.  
  4. public class DBconnect {
  5.  
  6.  
  7.  
  8. private static Connection conn = null;
  9. static Statement stmt = null;
  10.  
  11. public static Connection getConnection(){
  12.  
  13. if(DBconnect.conn == null){
  14. createConn();
  15. }
  16. return DBconnect.conn;
  17. }
  18.  
  19. private static void createConn()
  20. {
  21.  
  22. try{
  23. Class.forName("com.mysql.jdbc.Driver");
  24.  
  25. String url = "jdbc:mysql:///test";
  26. String username = "root";
  27. String pass = "101010";
  28.  
  29. DBconnect.conn = DriverManager.getConnection(url, username, pass);
  30.  
  31. stmt = conn.createStatement();
  32.  
  33.  
  34. }
  35. catch (ClassNotFoundException e){
  36. System.out.println(e.getMessage());
  37. System.exit(0);
  38. }
  39. catch (SQLException e){
  40. System.out.println(e.getMessage());
  41. System.exit(0);
  42. }
  43. catch (Exception e){
  44. }
  45.  
  46. }
  47.  
  48. }
Add Comment
Please, Sign In to add comment