Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package serverapp;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13.  
  14. /**
  15.  *
  16.  * @author admin
  17.  */
  18. public class TovarService {
  19.  
  20.     // select naim, sum(kol) from test.tovar group by naim order by naim    
  21.     //
  22.     private static Connection getConn() throws SQLException {
  23.         //
  24.         String connectionString = "jdbc:mariadb://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&user=root&password=";
  25.         final Connection connection = DriverManager.getConnection(connectionString);
  26.         return connection;
  27.     }
  28.     // метод для получения данных из таблицы tovar    
  29.     public TovarItem[] getItems() {
  30.         // получаем соединение с помощью метода getConn()
  31.         try (Connection c = getConn();
  32.              PreparedStatement s = c.prepareStatement("select naim, sum(kol) from test.tovar group by naim order by naim");
  33.              ResultSet rs = s.executeQuery();) {
  34.             while (rs.next()) {
  35.                 int tovarId = rs.getInt(1);
  36.                 int kol = rs.getInt(2);
  37.                 System.out.println("tovarId=" + tovarId);
  38.                 System.out.println("kol=" + kol);
  39.             }
  40.         } catch (Exception exc) {
  41.             exc.printStackTrace();
  42.         }
  43.         return null;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement