daily pastebin goal
80%
SHARE
TWEET

Untitled

a guest Sep 20th, 2016 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package demo.data;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. public class DAL {
  12.  
  13.     static String connectURL = "jdbc:mysql://localhost:3306/northwind?useSSL=false";
  14.  
  15.     public static List<Shipper> listAllShippers() {
  16.  
  17.         List<Shipper> shippers = new ArrayList<>();
  18.         try (Connection con = DriverManager.getConnection(connectURL, "root", "password")) {
  19.             try (PreparedStatement pst = con.prepareStatement("select * from shippers")) {
  20.                 try (ResultSet rs = pst.executeQuery()) {
  21.                     while (rs.next()) {
  22.                         shippers.add(new Shipper(rs.getInt("ShipperID"), rs.getString("CompanyName"),
  23.                                 rs.getString("Phone")));
  24.                     }
  25.                 }
  26.             }
  27.         } catch (SQLException e) {
  28.             e.printStackTrace();
  29.         }
  30.         return shippers;
  31.     }
  32.  
  33.     public static int addShipper(String compName, String phone) {
  34.         int post = 0;
  35.         try (Connection con = DriverManager.getConnection(connectURL, "root", "password")) {
  36.             try (PreparedStatement pst = con
  37.                     .prepareStatement("insert into shippers(CompanyName, Phone) values (?,?)")) {
  38.                 pst.setString(1, compName);
  39.                 pst.setString(2, phone);
  40.                 post = pst.executeUpdate();
  41.             }
  42.         } catch (SQLException e) {
  43.             e.printStackTrace();
  44.         }
  45.         return post;
  46.     }
  47. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top