Advertisement
sergAccount

Untitled

Mar 14th, 2021
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 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 testdb;
  7.  
  8. import java.sql.*;
  9.  
  10. public class DataBaseUtil {
  11.  
  12.     // метод для получения соединения !!!
  13.     // Connection - интерфейс для получения соединения с БД
  14.     public static Connection getConn() throws SQLException {
  15.         String connectionString = "jdbc:mariadb://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&user=root&password=";
  16.         final Connection connection = DriverManager.getConnection(connectionString);
  17.         return connection;
  18.     }
  19.     // select * from test.t2 t
  20.     public static void printTableData() {
  21.         // try-with-resources
  22.         String sql = "select * from test.t2 t";
  23.         try (Connection c = getConn();
  24.                 Statement s = c.createStatement();
  25.                 ResultSet rs = s.executeQuery(sql)) {
  26.             // ResultSet - интерфейс для получения данных в виде набора записей (строк)
  27.             while(rs.next()){
  28.                 String id = rs.getString("id");
  29.                 String name = rs.getString("name");
  30.                 System.out.println("printTableData.id="   + id);
  31.                 System.out.println("printTableData.name=" + name);
  32.             }
  33.         } catch (SQLException ex) {
  34.             ex.printStackTrace();
  35.         }
  36.     }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement