Advertisement
Guest User

Untitled

a guest
Oct 12th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. package DataBase;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.Statement;
  7.  
  8. public class DbConnection {
  9.     private Statement st;
  10.     private ResultSet rs;
  11.  
  12.     public DbConnection() {
  13.         try {
  14.             String url = "jdbc:mysql://localhost:3306/Shop";
  15.             String username = "root";
  16.             String password = "localhost";
  17.             Connection con = DriverManager.getConnection(url,username,password);
  18.             st = con.createStatement();
  19.             } catch (Exception ex) {
  20.         }
  21.     }
  22.  
  23.     public void getProducts() throws Exception {
  24.         String query = "select * from product";
  25.         rs = st.executeQuery(query);
  26.  
  27.         while(rs.next()){
  28.             String product = rs.getString("naam");
  29.             int prijs = rs.getInt("prijs");
  30.             System.out.println(product+":"+prijs);
  31.         }
  32.  
  33.     }
  34.  
  35.     public void updateTable(String query) throws Exception {
  36.  
  37.         try{
  38.             st.executeUpdate(query);
  39.         }
  40.         catch (Exception e){
  41.             System.out.println(e);
  42.         }
  43.  
  44.     }
  45.  
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement