STANAANDREY

db con

Jul 29th, 2021 (edited)
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. package com.company;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7. public class Main {
  8.     public static void main(String[] args) {
  9.         // Calea catre fisierul care va stoca baza de date. In cazul in care nu exista, acesta va fi creat.
  10.         String cale = "D:\\database\\db.sql";
  11.         // In cazul in care folosesti OS X sau Linux, va arata in genul "/home/user/tutorial/db.sql"
  12.         String url = "jdbc:sqlite:" + cale;
  13.  
  14.         try {
  15.             Connection connection = DriverManager.getConnection(url);
  16.             System.out.println("conectat cu succes");
  17.  
  18.             String query = "SELECT 1+1 AS result";
  19.             Statement statement = connection.createStatement();
  20.             System.out.println(statement.executeQuery(query).getInt("result")); // va afisa 2
  21.  
  22.             statement.close();
  23.             connection.close();
  24.         } catch (SQLException e) {
  25.             System.out.println(e.getMessage());
  26.         }
  27.     }
  28. }
Add Comment
Please, Sign In to add comment