Advertisement
Guest User

Untitled

a guest
Mar 9th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. package databased;
  2. import java.sql.*;
  3. // Do not import com.mysql.jdbc.*
  4.  
  5. public class DatabaseConnection {
  6. Connection conn = null;
  7. public DatabaseConnection() {
  8. try {
  9. Class.forName("com.mysql.jdbc.Driver").newInstance();
  10.  
  11. try {
  12. conn = DriverManager.getConnection("jdbc:mysql://mysql.stud.ntnu.no/oysteils_databased?" +
  13. "user=oysteils_tdt4145&password=hunter2");
  14. } catch (SQLException ex) {
  15. System.out.println("SQLException: " + ex.getMessage());
  16. System.out.println("SQLState: " + ex.getSQLState());
  17. System.out.println("VendorError: " + ex.getErrorCode());
  18. }
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. }
  23.  
  24. public static void viewTable(Connection con, String dbName)
  25. throws SQLException {
  26. //TODO
  27. Statement stmt = null;
  28. String query = "select COF_NAME, SUP_ID, PRICE, " +
  29. "SALES, TOTAL " +
  30. "from " + dbName + ".COFFEES";
  31. try {
  32. stmt = con.createStatement();
  33. ResultSet rs = stmt.executeQuery(query);
  34. while (rs.next()) {
  35. String coffeeName = rs.getString("COF_NAME");
  36. int supplierID = rs.getInt("SUP_ID");
  37. float price = rs.getFloat("PRICE");
  38. int sales = rs.getInt("SALES");
  39. int total = rs.getInt("TOTAL");
  40. System.out.println(coffeeName + "\t" + supplierID +
  41. "\t" + price + "\t" + sales +
  42. "\t" + total);
  43. }
  44. } catch (SQLException e ) {
  45. e.printStackTrace();
  46. } finally {
  47. if (stmt != null) { stmt.close(); }
  48. }
  49. }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement