Advertisement
sergAccount

Untitled

Mar 14th, 2021
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 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.  
  20.     // select * from test.t2 t
  21.     public static void printTableData() {
  22.         // try-with-resources
  23.         String sql = "select * from test.t2 t";
  24.         try (Connection c = getConn();
  25.                 Statement s = c.createStatement();
  26.                 ResultSet rs = s.executeQuery(sql)) {
  27.  
  28.         } catch (SQLException ex) {
  29.             ex.printStackTrace();
  30.         }
  31.     }
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement