Advertisement
Guest User

Untitled

a guest
May 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. import java.sql.DriverManager;
  2. import java.sql.SQLException;
  3.  
  4. import javax.servlet.ServletException;
  5.  
  6. import com.mysql.jdbc.Connection;
  7.  
  8.  
  9. public class SqlConnection {
  10.  
  11. final static String driverName = "com.mysql.jdbc.Driver";
  12. String url="jdbc:mysql://localhost/ksiegarnia";
  13. String uid="root";
  14. String pwd="********";
  15. private static Connection con=null;
  16.  
  17.  
  18. public Connection getConnection() throws ServletException{
  19. if(con==null){
  20. try{
  21. Class.forName(driverName);
  22. con=(Connection) DriverManager.getConnection(url,uid,pwd);
  23. System.out.println("Połączenie z bazą udane");
  24. }
  25. catch(Exception e){
  26. throw new ServletException("Brak połączenia z bazą",e);
  27. }
  28.  
  29. }
  30. return con;
  31. }
  32.  
  33.  
  34. public void close() throws SQLException {
  35. // TODO Auto-generated method stub
  36. con.close();
  37.  
  38. }
  39.  
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement