Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package databased;
- import java.sql.*;
- // Do not import com.mysql.jdbc.*
- public class DatabaseConnection {
- Connection conn = null;
- public DatabaseConnection() {
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- try {
- conn = DriverManager.getConnection("jdbc:mysql://mysql.stud.ntnu.no/oysteils_databased?" +
- "user=oysteils_tdt4145&password=hunter2");
- } catch (SQLException ex) {
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void viewTable(Connection con, String dbName)
- throws SQLException {
- //TODO
- Statement stmt = null;
- String query = "select COF_NAME, SUP_ID, PRICE, " +
- "SALES, TOTAL " +
- "from " + dbName + ".COFFEES";
- try {
- stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(query);
- while (rs.next()) {
- String coffeeName = rs.getString("COF_NAME");
- int supplierID = rs.getInt("SUP_ID");
- float price = rs.getFloat("PRICE");
- int sales = rs.getInt("SALES");
- int total = rs.getInt("TOTAL");
- System.out.println(coffeeName + "\t" + supplierID +
- "\t" + price + "\t" + sales +
- "\t" + total);
- }
- } catch (SQLException e ) {
- e.printStackTrace();
- } finally {
- if (stmt != null) { stmt.close(); }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement