Guest User

Untitled

a guest
Mar 5th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. package data;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. public class DB {
  9.  
  10. private static final String DRIVER = "com.mysql.jdbc.Driver";
  11. private static String URL = "jdbc:mysql://SKRIV IP HER:3306/cupcakeshop";
  12. // private static String URL = "jdbc:mysql://localhost:3306/cupcakeshop";
  13. private static String USER = "XXXXXX";
  14. private static String PASSWORD = "YYYYYY";
  15. // private static String USER = "root";
  16. // private static String PASSWORD = "root";
  17. private static Connection conn = null;
  18.  
  19. public static Connection getConnection() {
  20. if (conn == null) {
  21. try {
  22. Class.forName(DRIVER);
  23. conn = DriverManager.getConnection(URL, USER, PASSWORD);
  24. } catch (ClassNotFoundException | SQLException ex) {
  25. ex.printStackTrace();
  26. }
  27. }
  28. return conn;
  29. }
  30.  
  31. public static void main(String[] args) {
  32. //Test connection
  33. try {
  34. String sql = "select idCupcake, cupcakeName, idTopping, idBottom from cupcake";
  35. ResultSet rs = getConnection().prepareStatement(sql).executeQuery();
  36. while (rs.next()) {
  37. System.out.println(rs.getString("cupcakeName"));
  38. }
  39. } catch (SQLException ex) {
  40. ex.printStackTrace();
  41. }
  42. }
  43. }
Add Comment
Please, Sign In to add comment